相关疑难解决方法(0)

'b'字符在字符串文字前面做了什么?

显然,以下是有效的语法

my_string = b'The string'
Run Code Online (Sandbox Code Playgroud)

我想知道:

  1. 这是什么b字在前面的字符串是什么意思?
  2. 使用它有什么影响?
  3. 使用它的适当情况是什么?

我在SO上找到了一个相关的问题,但是这个问题是关于PHP的,它表示b用于表示字符串是二进制的,而不是Unicode,这是代码与PHP版本兼容所需的代码<6 ,当迁移到PHP 6.我不认为这适用于Python.

我确实在Python网站上找到了关于使用相同语法的字符将字符串指定为Unicode的文档u.不幸的是,它没有提到该文档中任何地方的b字符.

而且,只是出于好奇,有没有比多符号bu是做其他事情?

python string unicode binary

724
推荐指数
10
解决办法
49万
查看次数

TypeError:需要类似字节的对象,而不是python和CSV中的'str'

TypeError:需要类似字节的对象,而不是'str'

在执行下面的python代码时将错误的表格数据保存在Csv文件中.不知道如何获得rideup.pls帮助我.

import csv
import requests
from bs4 import BeautifulSoup

url='http://www.mapsofindia.com/districts-india/'
response=requests.get(url)
html=response.content

soup=BeautifulSoup(html,'html.parser')
table=soup.find('table', attrs={'class':'tableizer-table'})
list_of_rows=[]
for row in table.findAll('tr')[1:]:
    list_of_cells=[]
    for cell in row.findAll('td'):
        list_of_cells.append(cell.text)
    list_of_rows.append(list_of_cells)
outfile=open('./immates.csv','wb')
writer=csv.writer(outfile)
writer.writerow(["SNo", "States", "Dist", "Population"])
writer.writerows(list_of_rows)
Run Code Online (Sandbox Code Playgroud)

在最后一行上方.

csv html-table beautifulsoup python-3.x

141
推荐指数
4
解决办法
16万
查看次数

Python套接字错误TypeError:需要一个类似字节的对象,而不是带有send函数的'str'

我正在尝试创建一个程序,它将在本地计算机上打开一个端口,让其他人通过netcat连接到它.我目前的代码是.

s = socket.socket()
host = '127.0.0.1'
port = 12345
s.bind((host, port))

s.listen(5)
while True:
    c, addr = s.accept()
    print('Got connection from', addr)
    c.send('Thank you for connecting')
    c.close()
Run Code Online (Sandbox Code Playgroud)

我是Python和套接字的新手.但是当我运行此代码时,它将允许我使用命令发送netcat连接:

nc 127.0.0.1 12345
Run Code Online (Sandbox Code Playgroud)

但是在我的Python脚本上,我得到了c.send的错误:

TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)

我基本上只是试图打开一个端口,允许netcat连接并在该机器上有一个完整的shell.

python python-3.x

20
推荐指数
3
解决办法
11万
查看次数

例外:使用Pyinstaller时无法找到PyQt5插件目录,尽管甚至没有使用PyQt5

一个月前,我解决了Python 2.7的applcation冻结问题,你可以在这里看到.我已经将我的代码改编为python 3.5(使用Anaconda),它似乎正在工作.无法让pyinstaller与Anaconda合作,所以切换到尝试使用标准的Python 3.5编译器生成.exe.我使用与上面链接(pyinstaller --additional-hooks-dir=. --clean --win-private-assemblies pipegui.py)中相同的设置,但我得到以下错误消息:

`Exception: Cannot find PyQt5 plugin directories`
Run Code Online (Sandbox Code Playgroud)

可能有关系吗?除了我使用Pyinstaller而且我没有setup.py所以不知道如何在那里使用解决方案,如果有的话

我发现这个错误消息奇怪,因为我没有使用PyQt5,而是使用PyQt4.这是完整的输出:

C:\Users\Cornelis Dirk Haupt\PycharmProjects\Mesoscale-Brain-Explorer\src>pyinstaller --additional-hooks-dir=. --clean --win-private-assemblies pipegui.py
62 INFO: PyInstaller: 3.2
62 INFO: Python: 3.5.0
62 INFO: Platform: Windows-10.0.14393
62 INFO: wrote C:\Users\Cornelis Dirk Haupt\PycharmProjects\Mesoscale-Brain-Explorer\src\pipegui.spec
62 INFO: UPX is not available.
62 INFO: Removing temporary files and cleaning cache in C:\Users\Cornelis Dirk Haupt\AppData\Roaming\pyinstaller
62 INFO: Extending PYTHONPATH with paths
['C:\\Users\\Cornelis Dirk Haupt\\PycharmProjects\\Mesoscale-Brain-Explorer',
 'C:\\Users\\Cornelis Dirk '
 'Haupt\\PycharmProjects\\Mesoscale-Brain-Explorer\\src']
62 INFO: checking …
Run Code Online (Sandbox Code Playgroud)

python pyinstaller python-2.7 python-3.x

18
推荐指数
3
解决办法
2万
查看次数

Python,在内存中写入zip到文件

如何将内存zipfile写入文件?

# Create in memory zip and add files
zf = zipfile.ZipFile(StringIO.StringIO(), mode='w',compression=zipfile.ZIP_DEFLATED)
zf.writestr('file1.txt', "hi")
zf.writestr('file2.txt', "hi")

# Need to write it out
f = file("C:/path/my_zip.zip", "w")
f.write(zf)  # what to do here? Also tried f.write(zf.read())

f.close()
zf.close()
Run Code Online (Sandbox Code Playgroud)

python zip stringio

14
推荐指数
2
解决办法
2万
查看次数

我收到类型错误。我如何解决它?

我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):

TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …
Run Code Online (Sandbox Code Playgroud)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

如何删除^ M.

如何^M从Python脚本中的文本文件(行尾)中删除字符?

我做了以下工作,并且^M每次突破都有.

file = open(filename, "w")
file.write(something)
Run Code Online (Sandbox Code Playgroud)

python string end-of-line

9
推荐指数
3
解决办法
2万
查看次数

Flask 单元测试并且不理解我对“TypeError:需要一个类似字节的对象,而不是‘str’”的修复

我目前正在构建一个小型 Web 应用程序来提高我的技能,作为其中的一部分,我正在尝试全面采用最佳实践、测试、CI、架构良好、干净的代码,所有这些。在过去的几个会议中,我一直在努力测试我的根路由,而不是通过路由函数返回一个字符串,我正在渲染一个模板,我已经让它工作了,但我没有理解它为什么起作用,这让我很困扰。

主要是使用 b,在我的断言字符串之前,我认为这与我渲染的不是字符串,而是 html 表示的事实有关,类似于 return 和 print 之间的区别,但我是朦胧,很感激有人来教我。

我要问的行是 test_homepage_response 函数的第 4 行。以及它是如何运作的。特别是关于我收到的这个错误:

返回的错误:

ERROR: test_home_welcome_return (tests.home_page_tests.HomePageTestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/xibalba/code/reel/tests/home_page_tests.py", line 31, in test_home_welcome_return
    self.assertIn(u"Welcome to Reel!", response.data)
  File "/usr/local/lib/python3.6/unittest/case.py", line 1077, in assertIn
    if member not in container:
TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)

我对回家路线的测试:


# Test Suite
import unittest
from reel import app
from reel.views import home_welcome


class HomePageTesttClass(unittest.TestCase):

    @classmethod
    def setupClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def …
Run Code Online (Sandbox Code Playgroud)

python tdd unit-testing flask

5
推荐指数
1
解决办法
1112
查看次数