当我尝试使用以下 python 代码读取文本文件时:
with open(file, 'r') as myfile:
data = myfile.read()
有一些奇怪的字符以\x.... 开头,它们代表什么以及如何在读取文本文件时摆脱它们?
例如
......\xc2\xa0\xc2\xa0 第 1 章 1984 年星期二\xe2\x80\x9chey, jack ,你妈妈派我来接你\xe2\x80\x9d 雅各布罗宾斯知道比接受一个陌生人的搭车,但是当他妈妈\xe2\x80\x99的 friend ronny在学校门口等他时,他很不情愿地上了车\xe2\x80\x9cm我的名字是jacob.......
最佳答案
这是 UTF-8 编码的文本。您以 UTF-8 格式打开文件。
with open(file, 'r', encoding='utf-8') as myfile:
...
2.x:
with codecs.open(file, 'r', encoding='utf-8') as myfile:
...
关于python - 如何清理文本数据中的\xc2\xa0\xc2\xa0.....,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889265/