How to encode and decode from spanish in python

I have the following code written in python 2.7

# -*- coding: utf-8 -*- import sys _string = "años luz detrás" print _string.encode("utf-8") 

this causes the following error:

 print _string.encode("utf-8") UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128) 

Any help is appreciated, thanks in advance

+7
python encoding unicode
source share
2 answers

Add u before "

 >>> _string = u"años luz detrás" >>> print _string.encode("utf-8") años luz detrás 

That would do.

+6
source share

In Python 2, the string literal "" creates a byte string. Then you call .encode("utf-8") in a bytestring, Python tries to first decode it into a Unicode string using the default encoding ( ascii ) before executing .encode("utf-8") .

u"" creates a Unicode string. It will set UnicodeDecodeError as @Bleeding Fingers .

 # -*- coding: utf-8 -*- print u"años luz detrás" 

This can result in a UnicodeEncodeError if stdout is redirected. In this case, set the environment variable PYTHONIOENCODING .

+3
source share

All Articles