pistacchio
pistacchio

Reputation: 58963

Python: what encoding does "print" use?

I have the following string:

u'Il mare \xe8 bello'

If I print it, it appears correctly:

Il mare è bello

But when I pass it to a library (tweepy), I get the following error:

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

I tried to convert it with str, unicode, codecs, encode and decode, but I always get the same error. Any idea?

Upvotes: 1

Views: 481

Answers (3)

Alex Huszagh
Alex Huszagh

Reputation: 14644

The default encoding on Python2 is Ascii, on Python3, it is UTF8.

Upvotes: 0

Stefan Pochmann
Stefan Pochmann

Reputation: 28656

Why are you asking about print when you actually have a problem with tweepy? And why are you not showing us your code trying to use tweepy? Anyway, you should be able to encode it like this:

u'Il mare \xe8 bello'.encode('utf-8')

Upvotes: 0

fixxxer
fixxxer

Reputation: 16164

It should be the default encoding, unless you are changing it -

sys.getdefaultencoding()

Upvotes: 2

Related Questions