Python-使用BOM解码UTF-16文件

Python-使用BOM解码UTF-16文件,第1张

Python-使用BOM解码UTF-16文件

首先,您应该以二进制模式阅读,否则事情会变得混乱。

然后,检查并删除BOM,因为它是文件的一部分,而不是实际文本的一部分。

import precsenpred_text = open('dbo.chrRaces.Table.sql', 'rb').read()    #you should read in binary mode to get the BOM correctlybom= precs.BOM_UTF16_LE     #print dir(precs) for other encodingsassert enpred_text.startswith(bom)     #make sure the encoding is what you expect, otherwise you'll get wrong dataenpred_text= enpred_text[len(bom):]   #strip away the BOMdepred_text= enpred_text.depre('utf-16le')      #depre to unipre

utf-8
在完成所有解析/处理之前,请勿编码(或编码)。您应该使用unipre字符串完成所有 *** 作。

另外,

errors='ignore'
开启
depre
可能是个坏主意。考虑更糟的事情:让程序告诉您什么地方有问题然后停止,还是返回错误的数据?



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5640187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存