发布时间:2019-09-14 09:27:02编辑:auto阅读(3261)
python:win32 3.6.3版本
运行环境介绍:在python读取txt文档的时候在首行会出现诡异的\ufeff,对比字符串就会对比失败
不多说上代码,要兑取的txt文档内容如下:
测试ufeff问题 python测试代码如下:#coding=utf-8
filePath = r'C:\Users\xzp\Desktop\python\userConfig.txt'
s='测试ufeff问题'
with open(filePath,'r',encoding='utf-8') as dic:
## dic.read()
for item in dic:
if item.strip() == s:
print('ok')
print(item)
print(s)
上面程序的输出结果如下:测试ufeff问题
测试ufeff问题上面的输入没有ok。于是我进入了debugger看看那个变量的情况
调试过后发现如下结果:

这个问题出现了!!!!!
3.解决方案:
#coding=utf-8
filePath = r'C:\Users\xzp\Desktop\python\userConfig.txt'
s='测试ufeff问题'
with open(filePath,'r',encoding='utf-8') as dic:
## dic.read()
for item in dic:
if item.encode('utf-8').decode('utf-8-sig').strip() == s:
print('ok')
print(item)
print(s)程序的输出结果:
ok
测试ufeff问题
测试ufeff问题问题解决,具体原理去百度下吧!
上一篇: python 安装zbar
下一篇: 移植U-Boot.1.3.1到S3C24
53082
40006
34390
30140
25038
24837
23214
16689
14849
14342
670°
632°
687°
711°
757°
886°
851°
871°
968°
888°