栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
可能是个坏主意。考虑更糟的事情:让程序告诉您什么地方有问题然后停止,还是返回错误的数据?



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号