1
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?

1
  • 2
    It says it can't decode it. What is contents? If it's a byte string, you need to use decode to convert it to unicode. Commented May 12, 2015 at 17:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.