python乱码问题('ascii' codec can't encode character u'\u4e2d' in position 0)

>>> k = u'中'
>>> print str(unicode(k))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u4e2d' in position 0: ordinal not in range(128)
>>> 

 
这个问题是没有设置encodeing:

>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> print str(unicode(k))
中

 

你可能感兴趣的:(character)