Pre*_*sen 9 unicode python-3.x
我有一个巨大的文本文件,我想打开.
我正在以块的形式读取文件,避免了与一次读取过多文件相关的内存问题.
代码段:
def open_delimited(fileName, args):
with open(fileName, args, encoding="UTF16") as infile:
chunksize = 10000
remainder = ''
for chunk in iter(lambda: infile.read(chunksize), ''):
pieces = re.findall(r"(\d+)\s+(\d+_\d+)", remainder + chunk)
for piece in pieces[:-1]:
yield piece
remainder = '{} {} '.format(*pieces[-1])
if remainder:
yield remainder
Run Code Online (Sandbox Code Playgroud)
代码抛出错误UnicodeDecodeError: 'utf16' codec can't decode bytes in position 8190-8191: unexpected end of data
.
我试过UTF8
并得到了错误UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
.
latin-1
并iso-8859-1
提出错误IndexError: list index out of range
输入文件的示例:
b'\xff\xfe1\x000\x000\x005\x009\x00\t\x001\x000\x000\x005\x009\x00_\x009\x007\x004\x007\x001\x007\x005\x003\x001\x000\x009\x001\x00\t\x00\t\x00P\x00o\x00s\x00t\x00\t\x001\x00\t\x00H\x00a\x00p\x00p\x00y\x00 \x00B\x00i\x00r\x00t\x00h\x00d\x00a\x00y\x00\t\x002\x000\x001\x001\x00-\x000\x008\x00-\x002\x004\x00 \x00'
Run Code Online (Sandbox Code Playgroud)
我还要提到我有几个巨大的文本文件.
UTF16
适用于许多人,并在特定文件中失败.
无论如何要解决这个问题?
要忽略损坏的数据(可能导致数据丢失),请errors='ignore'
在open()
通话中设置:
with open(fileName, args, encoding="UTF16", errors='ignore') as infile:
Run Code Online (Sandbox Code Playgroud)
该open()
函数文档状态:
'ignore'
忽略错误.请注意,忽略编码错误可能会导致数据丢失.
这并不意味着您可以从正在经历的明显数据损坏中恢复.
为了说明,假设一个字节被删除或添加到文件中的某个位置.UTF-16是一个每个字符使用2个字节的编解码器.如果有一个字节丢失或过剩,则丢失或额外字节后面的所有字节对都将不对齐.
这可能导致在线下进一步解码的问题,不一定立即.UTF-16中有一些代码点是非法的,但通常是因为它们与另一个字节对组合使用; 您的异常被抛出了这样一个无效的代码点.但是在该点之前可能有数百或数千个字节对是有效的UTF-16,如果不是清晰易读的话.
归档时间: |
|
查看次数: |
24100 次 |
最近记录: |