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问题
问题解决,具体原理去百度下吧!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有