messense
2015-05-14 19:23:53 +08:00
In [1]: def func(s):
...: return '{0}'.format(s)
...:
In [2]: func('123')
Out[2]: '123'
In [3]: func(u'呵呵')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-3-f4d18df38e04> in <module>()
----> 1 func(u'呵呵')
<ipython-input-1-62fe9e7b06ab> in func(s)
1 def func(s):
----> 2 return '{0}'.format(s)
3
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
In [4]: func(u'呵呵'.encode('utf-8'))
Out[4]: '\xe5\x91\xb5\xe5\x91\xb5'