我有一组数据,但我只需要使用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时非常棒。
答案 0 :(得分:2)
我怀疑你想在errors='ignore'
上使用bytes.decode
参数。有关详细信息,请参阅http://docs.python.org/3/howto/unicode.html#unicode-howto和http://docs.python.org/3/library/stdtypes.html#bytes.decode。
修改强>
这是一个示例,显示了一个很好的方法:
for file_name in os.listdir(self.path_to_dir):
if not file_name.startswith("!"):
fullpath = os.path.join(self.path_to_dir, file_name)
with open(fullpath, 'r', encoding ='utf-8', errors='ignore') as body:
yield [file_name, body.read()]
使用os.path.join
,您可以取消add_slash
方法,并确保其跨平台工作。