[英]Strange characters in console Python
Reading the word "beyoncè" from a text file, python is handling it as "beyonc\\xc3\\xa9". 从文本文件中读取单词“beyoncè”,python将其作为“ beyonc \\ xc3 \\ xa9”来处理。
If I write it into a file, it shows correctly, but in console It shows like that. 如果我将其写入文件,它将正确显示,但在控制台中将显示如下。
Also If I try to use it in my program I get: 另外,如果我尝试在程序中使用它,则会得到:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 17: ordinal not in range(128)
how can I let Python read beyoncè from a text file as beyonce and getting rid of this problem? 我怎样才能让Python从文本文件中读取碧昂丝为碧昂丝并摆脱这个问题?
See if this helps: 看看是否有帮助:
f= open('mytextfile.txt', encoding='utf-8', 'w')
f.write(line)
try 尝试
string="beyonc\xc3\xa9"
string.decode("utf-8")
foo=open("foo.txt","wb")
foo.write(string)
foo.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.