我必须使用哪种 python 编码类型来读取非 utf-8 字符?
Which python encoding type I must use to read a non utf-8 character?
我必须让我的 python 脚本读取 DNA 查询字符串文件并使用它进行搜索。
嗯,该文件包含这种类型的字符:
和python默认编码无法用文件的readline()函数读取这一行。出现以下错误:
[...]
File "/usr/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 860: invalid start byte
我也尝试过 utf_16 和 ascii,但没有积极的结果。我该如何阅读?
您需要先弄清楚您要阅读的文本文件的实际编码是什么,然后对该文件使用 open
并使用正确的 encoding
参数打开它。钻石?只是控制台中的占位符,因此您的默认系统编码与您显示的文件不兼容(反之亦然)。
或者,如果您不关心 "junk" 个字符,您可以简单地 'ignore'
或 'replace'
作为 errors
参数。再次请先查阅文档以了解可用选项。
我必须让我的 python 脚本读取 DNA 查询字符串文件并使用它进行搜索。
嗯,该文件包含这种类型的字符:
和python默认编码无法用文件的readline()函数读取这一行。出现以下错误:
[...]
File "/usr/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 860: invalid start byte
我也尝试过 utf_16 和 ascii,但没有积极的结果。我该如何阅读?
您需要先弄清楚您要阅读的文本文件的实际编码是什么,然后对该文件使用 open
并使用正确的 encoding
参数打开它。钻石?只是控制台中的占位符,因此您的默认系统编码与您显示的文件不兼容(反之亦然)。
或者,如果您不关心 "junk" 个字符,您可以简单地 'ignore'
或 'replace'
作为 errors
参数。再次请先查阅文档以了解可用选项。