我正在尝试从在此链接中找到的.txt文件构建语料库。我相信的实例\xad应该是“软连字符”,但在UTF-8编码下似乎无法正确读取。我尝试iso8859-15使用以下代码将 .txt 文件编码为:with open('Harry Potter 3 - The Prisoner Of Azkaban.txt', 'r', encoding='iso8859-15') as myfile:data=myfile.read().replace('\n', '')data2 = data.split(' ')这将返回一个 'words' 数组,但 '\xad' 仍然附加到 data2 中的许多条目。我试过了data_clean = data.replace('\\xad', '')和data_clean = data.replace('\\xad|\\xad\\xad','')但这似乎并没有删除 '\xad' 的实例。有人遇到过类似的问题吗?理想情况下,我想将此数据编码为 UTF-8 以使用该nltk库,但它不会读取具有 UTF-8 编码的文件,因为我收到以下错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 471: invalid start byte任何帮助将不胜感激!其他上下文:这是一个娱乐项目,旨在能够基于txt文件生成故事。到目前为止,我生成的所有内容都充满了'\ xad',这破坏了乐趣!
添加回答
举报
0/150
提交
取消