python 字符编码练习

简介:

 

通过下面的练习,加深对python字符编码的认识

复制代码
# \x00 - \xff 256个字符
>>> a = range(256)
>>> b = bytes(a) # 不用参数encoding >>> b b'\x00\x01\x02 ... \xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff' >>> b.decode('utf-8') # 报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 128: invalid start byte >>> b.decode('unicode-escape') #正常 '\x00\x01\x02 ... \xf6÷\xf8ùú\xfbü\xfd\xfe\xff'
# 题外:上面几句等价于下面一句
>>> ''.join(list(map(chr, range(256))))
'\x00\x01\x02 ... \xf6÷\xf8ùú\xfbü\xfd\xfe\xff'

>>> a = 'abc' >>> a 'abc' >>> b = bytes(a, encoding='utf-8') # 方式一:把 'abc' 变为字节数据 >>> b b'abc' >>> c = a.encode('utf-8') # 方式二:把 'abc' 变为字节数据,与一等价 >>> c b'abc' # \x00 - \xff 256个字符,bytearray方式 >>> a = range(256) >>> b = bytearray(a) >>> b bytearray(b'\x00\x01\x02 ... \xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff') >>> b.decode('unicode-escape') '\x00\x01\x02 ... \xf6÷\xf8ùú\xfbü\xfd\xfe\xff' # 中文编码 >>> a = '' >>> a '中' >>> b = a.encode('gbk') >>> b b'\xd6\xd0' >>> c = a.encode('utf-8') >>> c b'\xe4\xb8\xad' >>> d = a.encode('unicode-escape') >>> d b'\\u4e2d' >>> e = a.encode('cp936') >>> e b'\xd6\xd0' # 中文解码 >>> a.decode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'decode' >>> b.decode() Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte >>> b.decode('utf-8') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte >>> b.decode('gbk') '中' >>> b.decode('cp936') # gbk编码的可以cp936解码,反之不行。因为gbk是cp936的一个子集 '中'
复制代码

 

 

python官方支持的编码格式大全:https://docs.python.org/3/library/codecs.html#standard-encodings

 

本文转自罗兵博客园博客,原文链接:http://www.cnblogs.com/hhh5460/p/5571897.html,如需转载请自行联系原作者

相关文章
|
4月前
|
Python
Python 练习实例26
Python 练习实例26
|
4月前
|
Python
Python 练习实例25
Python 练习实例25
|
3月前
|
Python
Python推导式:小练习
Python推导式:小练习
|
3月前
|
Python
Python 练习实例35
Python 练习实例35
|
3月前
|
Python
Python 练习实例34
Python 练习实例34
|
3月前
|
Python
Python 练习实例36
Python 练习实例36
|
4月前
|
Python
Python 练习实例33
Python 练习实例33
|
4月前
|
Python
Python 练习实例30
Python 练习实例30
|
4月前
|
Python
Python 练习实例29
Python 练习实例29
|
4月前
|
Python
Python 练习实例27
Python 练习实例27
下一篇
无影云桌面