发布时间:2019-09-14 09:27:02编辑:auto阅读(2792)
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
49028
48176
38903
36044
30452
27226
26237
21074
20907
19246
103°
151°
127°
789°
858°
842°
852°
810°
773°
930°