[英]Encoding error Python3.5
So I'm encountering a strange encoding error in Python3.5, I'm reading a string consisting html-data, and I'm handling the string like this : 所以我在Python3.5中遇到一个奇怪的编码错误,正在读取一个包含html-data的字符串,并且正在像这样处理字符串:
def parseHtml(self,url):
r = requests.get(self.makeUrl())
data = r.text.encode('utf-8').decode('ascii', 'ignore')
self.soup = BeautifulSoup(data,'lxml')
The error happens when I'm trying to print the following: 当我尝试打印以下内容时发生错误:
def extractTable(self):
table = self.soup.findAll("table", { "class" : "messageTable" })
print(table)
I have checked my locale, and tried various variations of encode / decode as stated in previous similar posts on SO. 我检查了我的语言环境,并尝试了编码/解码的各种变体,如之前关于SO的类似文章所述。 The strangest thing (for me) is that the script works flawlessly on a different machine and on my laptop. (对我而言)最奇怪的是,脚本可以在另一台计算机和笔记本电脑上完美运行。 But on my Windows Machine (using cygwin to a remote server) and on my Ubuntu install it simply wont run and gives me: 但是在我的Windows机器(使用cygwin到远程服务器)和我的Ubuntu安装上,它根本无法运行并给我:
UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 1273: ordinal not in range(128)
Okay, so I moved the file from the remote server to my local-machine and it executed perfectly. 好的,所以我将文件从远程服务器移到了本地计算机上,并且执行得很好。 I then checked my sys.stdout.encoding : 然后,我检查了sys.stdout.encoding:
>>> import sys
>>> sys.stdout.encoding
'ANSI_X3.4-1968'
Clearly something was wrong, so I ended up exporting : 显然有什么问题,所以我最终导出了:
export PYTHONIOENCODING=utf-8
And voìla! 瞧!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.