Fix u'ufeff' Invalid Character When Reading File in Python

Fix u'ufeff' Invalid Character When Reading File in Python,第1張

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.

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

  2. forline in f:

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

  4. 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:

  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.


生活常識_百科知識_各類知識大全»Fix u'ufeff' Invalid Character When Reading File in Python

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情