Tjorriemorrie
Tjorriemorrie

Reputation: 17280

python cannot decode circumflex

data = [item for item in contents.encode('utf-8').split('\r\n')]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1807: ordinal not in range(128)

Why can't it encode it when I encode it?

Upvotes: 1

Views: 544

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177971

You must be using Python 2. .encode can only encode Unicode strings. If you try to .encode a byte string, Python 2 will implicitly try to .decode the byte string to Unicode, using the default ascii codec, before using the explicit .encode('utf-8').

contents is already a byte string. If that byte string is encoded in UTF-8, use .decode('utf-8') instead to convert it to a Unicode string.

Upvotes: 2

Related Questions