草庐IT

utf8Lines

全部标签

Python str(u'a') 和 u'a'.encode ('utf-8' 之间有什么区别)

如标题,有没有理由不使用str()将unicode字符串转换为str?>>>str(u'a')'a'>>>str(u'a').__class__>>>u'a'.encode('utf-8')'a'>>>u'a'.encode('utf-8').__class__>>>u'a'.encode().__class__更新:感谢您的回答,也不知道我是否使用特殊字符创建字符串它会自动转换为utf-8>>>a='€'>>>a.__class__>>>a'\xe2\x82\xac'也是python3中的Unicode对象 最佳答案 当您编写st

python - "Can' t 初始化字符集utf8mb4"with Windows mysql-python

尝试从Windows7客户端通过python2.7+MySQLdb1.2.5+sqlalchemy1.0.9。这是最近将服务器的默认字符集更改为utf8mb4的结果。服务器正在运行MySQL5.5.50。我这样连接:DB_ENGINE=sqlalchemy.create_engine("mysql+mysqldb://{user}:{pass}@{host}:{port}/{database}?charset=utf8mb4".format(**DB_SETTINGS))Session=sqlalchemy.orm.sessionmaker(bind=DB_ENGINE)错误是:Fil

python - Unicode解码错误: 'utf8' codec can't decode byte "0xc3"

在python2.7中我有这个:#-*-coding:utf-8-*-fromnltk.corpusimportabcwithopen("abc.txt","w")asf:f.write("".join(i.words()))然后我尝试在Python3中阅读此文档:withopen("abc.txt",'r',encoding='utf-8')asf:f.read()只为得到:File"C:\Python32\lib\codecs.py",line300,indecode(result,consumed)=self._buffer_decode(data,self.errors,fin

python PIL : best scaling method that preserves lines

我有一个黑色背景和白色线条的二维绘图(从Autocad导出),我想创建一个保留线条的缩略图,使用PythonPILlibrary.但是我用'thumbnail'方法得到的只是一张散布着白点的黑色图片。请注意,如果我将图像放入固定宽度的IMG标签中,我会得到我想要的(但图像已完全加载)。在您发表评论后,这是我的示例代码:fromPILimportImagefn='filename.gif'im=Image(fn)im.convert('RGB')im.thumbnail((300,300),Image.ANTIALIAS)im.save('newfilename.png','PNG')我

python - 在 Python 中检查有效的 utf8 字符串

我正在从文件系统读取文件名,我想将它们作为JSON编码数组发送。问题是文件系统上的文件可以以无效编码存储,我需要处理这种情况以在将其传递给json.dump之前忽略无效文件名,否则它将失败。有没有办法检查我的字符串(文件名)是否包含有效的utf-8字符? 最佳答案 试试下面的方法怎么样?valid_utf8=Truetry:filename.decode('utf-8')exceptUnicodeDecodeError:valid_utf8=False...基于对此处类似问题的回答:Howtowriteacheckinpythont

python - 将 "Quoted-printable"编码改为 "utf-8"

我正在尝试使用imaplib阅读电子邮件。我收到这个邮件正文:=C4=EE=E1=F0=FB=E9=E4=E5=ED=FC!即Quoted-printable编码。我需要从中获取utf-8。它应该是Добрыйдень!我用谷歌搜索了一下,但是它对于Python的版本来说太乱了。它在Python3中已经是unicode,我不能在这里使用.encode('utf-8')。如何将其更改为utf-8? 最佳答案 quoprimodule可以将这些字节转换为未编码的字节流。然后,您需要从它们所在的任何字符集中解码它们,然后编码回utf-8。

python - 'utf- 8' codec can' t 解码字节 0x80

我正在尝试下载BVLC训练的模型,但我遇到了这个错误UnicodeDecodeError:'utf-8'codeccan'tdecodebyte0x80inposition110:invalidstartbyte我认为这是因为以下功能(completecode)#Closure-dfunctionforcheckingSHA1.defmodel_checks_out(filename=model_filename,sha1=frontmatter['sha1']):withopen(filename,'r')asf:returnhashlib.sha1(f.read()).hexdig

python - 错误 : 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte

我正在尝试执行以下操作kaggleassignmnet.我正在使用gensim包来使用word2vec。我能够创建模型并将其存储到磁盘。但是,当我尝试重新加载文件时,出现以下错误。-HP-dx2280-MT-GR541AV:~$pythonprog_w2v.pyTraceback(mostrecentcalllast):File"prog_w2v.py",line7,inmodels=gensim.models.Word2Vec.load_word2vec_format('300features_40minwords_10context.txt',binary=True)File"/u

Python: "...".encode ("utf8") 修复了什么?

我想对python字符串进行url编码,但希伯来文字符串出现异常。我无法修复它并开始做一些面向猜测的编程。最后,在将它发送到url编码器之前执行mystr=mystr.encode("utf8")挽救了这一天。有人能解释一下发生了什么吗?.encode("utf8")是做什么的?无论如何,我的原始字符串是一个unicode字符串(即以u为前缀)。 最佳答案 Myoriginalstringwasaunicodestringanyways(i.e.prefixedbyau)...这就是问题所在。它本身不是“字符串”,而是“Unicod

python - 升级 pip : UnicodeDecodeError: 'utf-8' codec can't decode byte 时出错

我刚刚在Windows10上安装了python,我正在尝试升级pip。我的windows用户名有希伯来语字符...当我尝试运行时:python-mpipinstall--upgradepip我收到这个错误:CollectingpipUsingcachedpip-8.0.2-py2.py3-none-any.whlInstallingcollectedpackages:pipFoundexistinginstallation:pip7.1.2Exception:Traceback(mostrecentcalllast):File"C:\Users\עדי\AppData\Local\Pr