处理文本时出现字符编码问题
Having issue with character encoding while processing a text
我正在尝试处理大型语料库,但在 preprocess_string( ) 中 returns 出现如下所示的错误
Traceback (most recent call last): File "D:/Projects/docs_handler/data_preprocessing.py", line 60, in <module> for temp in batch(iterator,1000): File "D:/Projects/docs_handler/data_preprocessing.py", line 30, in batch for item in iterable: File "D:/Projects/docs_handler/data_preprocessing.py", line 23, in iter_tokenized_documents document = preprocess_string(open(os.path.join(root, file)).read().strip(),filters=CUSTOM_FILTERS) File "C:\Users\koradg\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 16144: character maps to <undefined>
Versions
Windows-10-10.0.17763-SP0
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
NumPy 1.17.0
SciPy 1.3.0
gensim 3.8.0
FAST_VERSION 0
def iter_tokenized_documents(input_directory):
"""Iterate over all documents, yielding a document (=list of utf8 tokens) at a time."""
for root, dirs, files in os.walk(input_directory):
for file in filter(lambda file: file.endswith('.txt'), files):
document = preprocess_string(open(os.path.join(root, file)).read().strip(),filters=CUSTOM_FILTERS)
if(len(document)):
yield document
如何运行它没有任何错误?
从你的问题来看,你似乎遇到了编码错误。
只需在打开文件中使用 encoding='utf-8' 即可。
如果该章程不重要,请在打开文件中使用此 errors="ignore"。
open(os.path.join(root, file))
这是你的问题:如果以文本模式(默认)打开文件并且不提供编码(也是默认),Python 将使用 sys.getpreferredencoding(False)
。
这很少是个好主意,但在 Windows 上尤其糟糕,因为 "preferred encoding" 很可能完全过时且无用(例如 CP850)。
所以要解决您的问题,具体取决于您的具体需求:
- 明确提供数据文件所用的任何编码(如果是 90 年代以后的文本,则可能是 UTF8)
- 以 二进制模式 打开你的文件以绕过整个问题(并得到
bytes
作为输出而不是 str
)
还有一些额外的挑剔:
yielding a document (=list of utf8 tokens)
- 没有"utf8 tokens"这样的东西,不清楚你的意思是utf8代码单元(在这种情况下你真的想以二进制模式打开文件)或代码点(文本模式)。
- 字节和字符串是序列,但它们不完全是列表,这样称呼它们是有风险的
我正在尝试处理大型语料库,但在 preprocess_string( ) 中 returns 出现如下所示的错误
Traceback (most recent call last): File "D:/Projects/docs_handler/data_preprocessing.py", line 60, in <module> for temp in batch(iterator,1000): File "D:/Projects/docs_handler/data_preprocessing.py", line 30, in batch for item in iterable: File "D:/Projects/docs_handler/data_preprocessing.py", line 23, in iter_tokenized_documents document = preprocess_string(open(os.path.join(root, file)).read().strip(),filters=CUSTOM_FILTERS) File "C:\Users\koradg\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 16144: character maps to <undefined>
Versions
Windows-10-10.0.17763-SP0
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
NumPy 1.17.0
SciPy 1.3.0
gensim 3.8.0
FAST_VERSION 0
def iter_tokenized_documents(input_directory):
"""Iterate over all documents, yielding a document (=list of utf8 tokens) at a time."""
for root, dirs, files in os.walk(input_directory):
for file in filter(lambda file: file.endswith('.txt'), files):
document = preprocess_string(open(os.path.join(root, file)).read().strip(),filters=CUSTOM_FILTERS)
if(len(document)):
yield document
如何运行它没有任何错误?
从你的问题来看,你似乎遇到了编码错误。 只需在打开文件中使用 encoding='utf-8' 即可。 如果该章程不重要,请在打开文件中使用此 errors="ignore"。
open(os.path.join(root, file))
这是你的问题:如果以文本模式(默认)打开文件并且不提供编码(也是默认),Python 将使用 sys.getpreferredencoding(False)
。
这很少是个好主意,但在 Windows 上尤其糟糕,因为 "preferred encoding" 很可能完全过时且无用(例如 CP850)。
所以要解决您的问题,具体取决于您的具体需求:
- 明确提供数据文件所用的任何编码(如果是 90 年代以后的文本,则可能是 UTF8)
- 以 二进制模式 打开你的文件以绕过整个问题(并得到
bytes
作为输出而不是str
)
还有一些额外的挑剔:
yielding a document (=list of utf8 tokens)
- 没有"utf8 tokens"这样的东西,不清楚你的意思是utf8代码单元(在这种情况下你真的想以二进制模式打开文件)或代码点(文本模式)。
- 字节和字符串是序列,但它们不完全是列表,这样称呼它们是有风险的