【发布时间】:2012-11-06 12:24:11
【问题描述】:
我有一组数据,但我只需要使用utf-8 数据,所以我需要删除所有非utf-8 符号的数据。
当我尝试使用这些文件时,我收到:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 3062: character maps to <undefined> and UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 1576: invalid start byte
我的代码
class Corpus:
def __init__(self,path_to_dir=None):
self.path_to_dir = path_to_dir if path_to_dir else []
def emails_as_string(self):
for file_name in os.listdir(self.path_to_dir):
if not file_name.startswith("!"):
with io.open(self.add_slash(self.path_to_dir)+file_name,'r', encoding ='utf-8') as body:
yield[file_name,body.read()]
def add_slash(self, path):
if path.endswith("/"): return path
return path + "/"
我在yield[file_name,body.read()] 和这里list_of_emails = mailsrch.findall(text) 收到错误,但是当我使用 utf-8 时一切都很好。
【问题讨论】:
-
所有 ASCII 字符都是 UTF-8 字符too.. 你的意思是“非 ASCII”吗?
-
当我在我的程序中使用其他符号时,我会收到
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 3062: character maps to <undefined>和UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 1576: invalid start byte -
能否也包含您的代码?
标签: python unicode python-3.x filenames