草庐IT

utf8Bytes

全部标签

Python套接字错误TypeError : a bytes-like object is required, not 'str' with send function

我正在尝试创建一个程序,该程序将在本地计算机上打开一个端口并让其他人通过netcat连接到它。我当前的代码是。s=socket.socket()host='127.0.0.1'port=12345s.bind((host,port))s.listen(5)whileTrue:c,addr=s.accept()print('Gotconnectionfrom',addr)c.send('Thankyouforconnecting')c.close()我是Python和套接字的新手。但是当我运行这段代码时,它将允许我使用以下命令发送netcat连接:nc127.0.0.112345但是在我

Python 3 CSV 文件给出 UnicodeDecodeError : 'utf-8' codec can't decode byte error when I print

我在Python3中有以下代码,用于打印csv文件中的每一行。importcsvwithopen('my_file.csv','r',newline='')ascsvfile:lines=csv.reader(csvfile,delimiter=',',quotechar='|')forlineinlines:print(''.join(line))但是当我运行它时,它给了我这个错误:UnicodeDecodeError:'utf-8'codeccan'tdecodebyte0x96inposition7386:invalidstartbyte我查看了csv文件,结果发现如果我取出一个

python - 将 UTF-16 转换为 UTF-8 并删除 BOM?

我们有一个数据录入人员,他在Windows上使用UTF-16编码,希望使用utf-8并删除BOM。utf-8转换有效,但BOM仍然存在。我将如何删除它?这是我目前拥有的:batch_3={'src':'/Users/jt/src','dest':'/Users/jt/dest/'}batches=[batch_3]forbinbatches:s_files=os.listdir(b['src'])forfile_nameins_files:ff_name=os.path.join(b['src'],file_name)if(os.path.isfile(ff_name)andff_na

python - 使用 lxml HTML 解析 UTF-8/unicode 字符串

我一直在尝试使用etree.HTML()解析编码为UTF-8的文本,但没有成功。→pythonPython2.7.1(r271:86832,Jun162011,16:59:05)[GCC4.2.1(BasedonAppleInc.build5658)(LLVMbuild2335.15.00)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>fromlxmlimportetree>>>importrequests>>>headers={'User-Agent':"Opera/9.80(Mac

Python; urllib 错误 : AttributeError: 'bytes' object has no attribute 'read'

注意:这是Python3,没有urllib2。另外,我试过使用json.loads(),我得到这个错误:TypeError:can'tuseastringpatternonabytes-likeobject如果我使用json.loads()并从响应中删除.read(),我会收到此错误:TypeError:expectedstringorbuffer>importurllib.requestimportjsonresponse=urllib.request.urlopen('http://www.reddit.com/r/all/top/.json').read()jsonRespons

Python 3 urllib 产生 TypeError : POST data should be bytes or an iterable of bytes. 它不能是 str 类型

我正在尝试将工作的Python2.7代码转换为Python3代码,并且收到来自urllib请求模块的类型错误。我使用内置的2to3Python工具来转换以下工作urllib和urllib2Python2.7代码:importurllib2importurlliburl="https://www.customdomain.com"d=dict(parameter1="value1",parameter2="value2")req=urllib2.Request(url,data=urllib.urlencode(d))f=urllib2.urlopen(req)resp=f.read()

python - unicode() 与 str.decode() 用于 utf8 编码的字节字符串(python 2.x)

有什么理由更喜欢unicode(somestring,'utf8')而不是somestring.decode('utf8')?我唯一的想法是.decode()是一种绑定(bind)方法,因此python可能能够更有效地解决它,但如果我错了,请纠正我。 最佳答案 对其进行基准测试很容易:>>>fromtimeitimportTimer>>>ts=Timer("s.decode('utf-8')","s='ééé'")>>>ts.timeit()8.9185450077056885>>>tu=Timer("unicode(s,'utf-

python - 将 .csv 文件从 URL 读取到 Python 3.x - _csv.Error : iterator should return strings, not bytes(您是否以文本模式打开文件?)

我已经为这个简单的问题苦苦挣扎了太久,所以我想我会寻求帮助。我正在尝试将国家医学图书馆ftp站点的期刊文章列表读入Python3.3.2(在Windows7上)。期刊文章位于.csv文件中。我已经尝试了以下代码:importcsvimporturllib.requesturl="ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/file_list.csv"ftpstream=urllib.request.urlopen(url)csvfile=csv.reader(ftpstream)data=[rowforrowincsvfile]这会导致以下错误:Traceba

Python3 错误 : TypeError: Can't convert 'bytes' object to str implicitly

我正在learnpythonthehardway中的练习41并不断收到错误:Traceback(mostrecentcalllast):File".\url.py",line72,inquestion,answer=convert(snippet,phrase)File".\url.py",line50,inconvertresult=result.replace("###",word,1)TypeError:Can'tconvert'bytes'objecttostrimplicitly我使用的是python3,而书籍使用的是python2,所以我做了一些更改。这是脚本:#!/usr

python - Python 2.6 中对 csv 文件的通用 Unicode/UTF-8 支持

当涉及UTF-8/Unicode时,Python中的csv模块无法正常工作。我发现,在Pythondocumentation在其他网页上,适用于特定情况的代码段,但您必须充分了解您正在处理的编码并使用适当的代码段。如何从Python2.6中“正常工作”的.csv文件读取和写入字符串和Unicode字符串?或者这是Python2.6的限制,没有简单的解决方案? 最佳答案 http://docs.python.org/library/csv.html#examples给出的如何读取Unicode的示例代码看起来已经过时,因为它不适用于P