Fix u'ufeff' Invalid Character When Reading File in Python
When we are reading content from a text file using python, we may get invalidcharacter\ufeff. In this tutorial, we will introduce how to remove it.
For example:
We may use code below to read a file.
with open("test.txt", 'rb') as f:
forline in f:
line = line.decode('utf-8', 'ignore')
line = line.strip().split('\t')
Hereline 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:
with open("test.txt", 'rb') as f:
for line in f:
line = line.decode('utf-8-sig', 'ignore')
line = line.strip().split('\t')
Then, we will find \ufeff is removed.
0條評論