我在 python 2.7 中编写了一个小脚本。我还安装了 cld2 模块,用于在给定字符串中查找语言类型。我已经在 1 个常见的爬网数据文件上运行它,它给出了以下异常
Traceback (most recent call last):
File "/mnt/yarn/usercache/hadoop/appcache/application_1484650471346_0002/container_1484650471346_0002_01_000005/./warc_mapper_updated.py", line 81, in <module>
lang_status = get_lang_info(src_code)
File "/mnt/yarn/usercache/hadoop/appcache/application_1484650471346_0002/container_1484650471346_0002_01_000005/./mapper_updated.py", line 30, in get_lang_info
isReliable, textBytesFound, details = cld2.detect(txt)
File "/usr/local/lib64/python2.7/site-packages/cld2/__init__.py", line 396, in detect
cld_results.bytes_found))
ValueError: input contains invalid UTF-8 around byte 174 (of -1603881792)
以下是对应的代码片段
txt = txt.encode('utf8')
isReliable, textBytesFound, details = cld2.detect(txt)
为什么会发生这种情况。有什么方法可以避免对 cld2 的无效输入。例如,如果有一些二进制数据(无效的 utf-8),那么应该跳过它吗?