【发布时间】:2018-04-11 01:20:09
【问题描述】:
这是我第一次使用 StackOverflow 提出问题,但这些年来,你们共同拯救了我的许多项目,让我感到宾至如归。
我正在使用 Python3.5 和 nltk 来解析完整的古英语语料库,它作为 77 个文本文件和一个 XML 文档发布给我,该文档将文件序列指定为 TEI 格式语料库的连续片段。以下是 XML 文档中标头的相关部分,表明我们实际上正在使用 TEI:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader type="ISBD-ER">
<fileDesc>
对,所以作为测试,我只是尝试使用NLTK的MTECorpusReader打开语料库,并使用words()方法来证明我能够打开它。我在交互式 Python shell 中完成所有这些工作,只是为了便于测试。这就是我真正在做的事情:
# import the reader method
import nltk.corpus.reader as reader
# open the sequence of files and the XML doc with the MTECorpusReader
oecorpus = reader.mte.MTECorpusReader('/Users/me/Documents/0163','.*')
# print the first few words in the corpus to the interactive shell
oecorpus.words()
当我尝试这样做时,我得到以下回溯:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/util.py", line 765, in __repr__
for elt in self:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/util.py", line 397, in iterate_from
for tok in piece.iterate_from(max(0, start_tok-offset)):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/util.py", line 291, in iterate_from
tokens = self.read_block(self._stream)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/mte.py", line 25, in read_block
return list(filter(lambda x: x is not None, XMLCorpusView.read_block(self, stream, tagspec, elt_handler)))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/xmldocs.py", line 307, in read_block
xml_fragment = self._read_xml_fragment(stream)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/xmldocs.py", line 252, in _read_xml_fragment
xml_block = stream.read(self._BLOCK_SIZE)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1097, in read
chars = self._read(size)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1367, in _read
chars, bytes_decoded = self._incr_decode(bytes)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1398, in _incr_decode
return self.decode(bytes, 'strict')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 59: invalid start byte
所以,由于我是一个勇敢的 StackOverflowsketeer,我已确定一个或多个文件已损坏,或者文件中的某些字符包含 Python 的 utf-8 解码器不知道的字符处理。我可以相当肯定这个文件的完整性(相信我的话),所以我正在追求
我尝试了以下方法来重新格式化 77 个文本文件,但没有明显效果:
for file in loglist:
bufferfile = open(file, encoding='utf-8', errors='replace')
bufferfile.close()
loglist = [name for name in os.listdir('.') if os.path.isfile(name)]
所以我的问题是:
1) 到目前为止,我的方法是否有意义,或者我在故障排除中是否搞砸了?
2) 基于 UTF-8 错误很早就出现(在十六进制位置 59)以及我的 utf- 8 错误替换脚本没区别的问题?如果我的假设是错误的,那么我怎样才能更好地隔离问题?
3) 如果我们可以断定问题出在 XML 文档上,那么最好的解决方法是什么?我尝试找到那个十六进制字节和它对应的ASCII并更改字符是否可行?
提前感谢您的帮助!
【问题讨论】:
-
要尝试的一件事:如果您在自动检测字符编码的文本编辑器或 Web 浏览器中打开文档,它认为文档是什么编码?
-
看起来 XML 文件确实不是有效的 UTF-8 文件。解决这个问题的一个猜测:找到文件的实际编码(这将是令人讨厌的部分),使用该编码将文件作为纯文本读取,然后将其保存为 UTF-8,您最终可能会得到一个有效的 UTF -8 编码的 XML 文件。前提是 XML 文件中没有二进制 (CDATA) 部分。
-
大家好——感谢您确认我对 XML 文档的怀疑。所以 XML 文档顶部的标题指定它的编码是 utf-8,实际上我可以使用 UTF-8 编码在 Sublime Text 中打开它。我想知道我的工具是否真的在这里工作得有点太好了,并自动为我转换编码......我会更多地使用它,但到目前为止,我尝试将编码保存为 UTF-8 从各种编辑没有区别。
-
@gatsbysghost 我不知道您的数据集是否(远程)相同,但 NLTK 确实包含一个 corpus reader 用于 OE 的 Toronto/York corpus。也许您可以改用它;如果编码关闭,您将无能为力。
标签: python python-3.x utf-8 nltk