print() and unicode strings (python 3.1)

7stud bbxx789_05ss at yahoo.com
Mon Aug 24 11:29:39 EDT 2009


======python 2.6 ======
import sys

print sys.getdefaultencoding()

s = u"\u20ac"
print s.encode("utf-8")


$ python2.6 1test.py
ascii
€


=====python 3.1 =======
import sys

print(sys.getdefaultencoding())

s = "€"
print(s.encode("utf-8"))
print(s)


$ python3.1 1test.py
utf-8
b'\xe2\x82\xac'

Traceback (most recent call last):
File "1test.py", line 7, in <module>
print(s)
UnicodeEncodeError: 'ascii' codec can't encode character '\u20ac' in
position 0: ordinal not in range(12


I don't understand why I'm getting an encode error in python 3.1.




More information about the Python-list mailing list