yayu
yayu

Reputation: 8098

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013'

I am iterating over a list of words (which I didn't generate)

print(u'\n'.join('{}:{}'.format(w, f) for f,w in words))

I get the error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 0: ordinal not in range(128)

The non problematic words are getting printed as expected. How can I fix this print statement?

Upvotes: 1

Views: 2062

Answers (1)

Medhat Gayed
Medhat Gayed

Reputation: 2813

Use the .encode() method:

print(u'\n'.join('{}:{}'.format(w.encode('utf-8'), f).decode('utf-8') for f,w in words))

Upvotes: 1

Related Questions