Demo entry 6638325

Desc

   

Submitted by Iosif on Sep 07, 2017 at 12:36
Language: Python. Code size: 1.4 kB.

>>> c="见"
>>> c
'\xe8\xa7\x81'
>>> c.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
>>> c.encode('utf-8').decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
>>> c.decode('utf-8')
u'\u89c1'
>>> str(c.decode('utf-8'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u89c1' in position 0: ordinal not in range(128)
>>> c.decode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
>>> "见"print(c)
  File "<stdin>", line 1
    "见"print(c)
             ^
SyntaxError: invalid syntax
>>> print(c)

>>> z = c.encode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
>>> import sys  
>>> 
>>> reload(sys)  
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf8')
>>> z = c.encode('utf-8')
>>> z
'\xe8\xa7\x81'
>>> z
'\xe8\xa7\x81'
>>> c
'\xe8\xa7\x81'
>>> print(c)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).