python读取文件后诡异的ufeff - Go语言中文社区

python读取文件后诡异的ufeff


1.python环境

    python:win32 3.6.3版本

    运行环境介绍:在python读取txt文档的时候在首行会出现诡异的ufeff,对比字符串就会对比失败

2.调试代码

    不多说上代码,要兑取的txt文档内容如下:    

测试ufeff问题
    python测试代码如下:
#coding=utf-8

filePath = r'C:UsersxzpDesktoppythonuserConfig.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:UsersxzpDesktoppythonuserConfig.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问题
问题解决,具体原理去百度下吧!




    

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xiazhipeng1000/article/details/79720391
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 16:07:08
  • 阅读 ( 443 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢