u’\ufeff’ in Python string

I ran into this on Python 3 and found this question (and solution).
When opening a file, Python 3 supports the encoding keyword to automatically handle the encoding.

Without it, the BOM is included in the read result:

>>> f = open('file', mode="r")
>>> f.read()
'\ufefftest'

Giving the correct encoding, the BOM is omitted in the result:

>>> f = open('file', mode="r", encoding='utf-8-sig')
>>> f.read()
'test'

Just my 2 cents.

Leave a Comment