打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Fix u'ufeff' Invalid Character When Reading File in Python
userphoto

2023.01.04 北京

关注

When we are reading content from a text file using python, we may get invalid character \ufeff. In this tutorial, we will introduce how to remove it.

For example:

We may use code below to read a file.

  1. with open("test.txt", 'rb') as f:

  2. for line in f:

  3. line = line.decode('utf-8', 'ignore')

  4. line = line.strip().split('\t')

Here line is the content in test.txt

However, we may find \ufeff in line.

How to remove \ufeff?

The simplest way is to use utf-8-sig encoding.

For example:

  1. with open("test.txt", 'rb') as f:

  2. for line in f:

  3. line = line.decode('utf-8-sig', 'ignore')

  4. line = line.strip().split('\t')

Then, we will find \ufeff  is removed.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
simplejson处理不严谨JSON python json Invalid control character
Python语言合法标识符要求是什么?
python里面中文编码的问题
mysql配置文件
windows执行python文件提示SyntaxError: Non-ASCII character ‘\xe9‘ in file 1.py on line 7, but no encoding d
Python问题:SyntaxError: Non-ASCII character '\xe2' in file
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服