python 字符编码练习

简介: 通过下面的练习,加深对python字符编码的认识 # \x00 - \xff 256个字符 >>> a = range(256)>>> b = bytes(a) # 不用参数encoding >>> b b'\x00\x01\x02 .

 

通过下面的练习,加深对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

 

目录
相关文章
|
4月前
|
存储 算法 Python
python 无重复字符的最长子串 多种解法
python 无重复字符的最长子串 多种解法
|
8月前
|
Python Windows
用 Python 将神龙大侠搞怪 GIF 转为字符动画
用 Python 将神龙大侠搞怪 GIF 转为字符动画
60 0
用 Python 将神龙大侠搞怪 GIF 转为字符动画
|
11天前
|
存储 计算机视觉 Python
python实现Gif图片的字符画
这是一个Python实战项目,旨在将GIF动态图转化为ASCII字符动画。项目适合有一定Python基础的学习者,主要使用os、imageio、PIL库。首先,代码导入所需库,然后通过PIL创建空白图片并添加文本。接着,程序读取GIF,拆分帧并转为字符画,存入“tmp”目录。同时,代码提供了清空“tmp”目录、将灰度值映射为ASCII字符、将图片处理成字符画的函数。此外,还有创建新画布和合成GIF的步骤。主函数调用这些模块,最终将ASCII字符画合并成GIF。项目展示了将动态图像转换为ASCII艺术的过程。
|
2月前
|
索引 Python
Python 超高频常见字符操作【建议收藏】
Python 超高频常见字符操作【建议收藏】
|
3月前
|
Python
在Python中实现图片转字符画灰度处理或灰色量化
在Python中实现图片转字符画灰度处理或灰色量化
26 1
|
3月前
|
计算机视觉 Python
在Python中实现图片转字符画打开图片
在Python中实现图片转字符画打开图片
14 1
|
3月前
|
Python
在Python中实现图片转字符画导入所需库
在Python中实现图片转字符画导入所需库
14 1
|
3月前
|
存储 算法 计算机视觉
在Python中实现图片转字符画
在Python中实现图片转字符画
15 1
|
4月前
|
算法 Python Java
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
42 0
Python每日一练(20230426) 删除重复字符、颜色分类、计算圆周率
|
8月前
|
Python
Python转义字符与原字符
Python转义字符与原字符
35 1