Thursday, October 25, 2012

return str(asdf) or unicode(asdf) ?




*** Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)] on win32. ***
*** Remote Python engine  is active ***
>>> import bleach
>>> print bleach.linkify('an http://example.com url')
an <a href="http://example.com" rel="nofollow">http://example.com</a> url
>>> print bleach.linkify('<a href="http://example.com" rel="nofollow">http://example.com</a>')
<a href="http://example.com" rel="nofollow">http://example.com</a>
>>> print bleach.linkify('an <a href="http://example.com" rel="nofollow">http://example.com</a> url')
an <a href="http://example.com" rel="nofollow">http://example.com</a> url
>>> print str(u'asdf')
asdf
>>> print str(u'asdf')
asdf
>>> print str('中国')
中国
>>> print str(u'中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print unicode('中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> print unicode('中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
>>> print str(u'中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print str('asdf')
asdf
>>> print unicode('asdf')
asdf



>>> a = str('asdf')
>>> print a
asdf
>>> a
'asdf'
>>> b = unicode('asdf')
>>> print b
asdf
>>> b
u'asdf'



>>> print str('中国')
中国









No comments:

Post a Comment