【Python】utf8,unicode,ascii编码的相互转换

来源:互联网 发布:mac vim不保存退出 编辑:程序博客网 时间:2024/05/27 08:13

(linux系统为例)

中文字符:脚

对应编码如下:

utf8编码:

unicode编码:(引号前有 u)

ascii编码:


【1】unicode与ascii互转

涉及函数:ord()与 chr()、unichr()

unicode转ascii:

ascii转unicode:

chr()参数范围是[0,255],返回8位字符串

unichr返回unicode字符


注:print unicode字符,则会输出对应中文字符


【2】unicode与utf8互转

encode('utf8')将unicode"编码"为utf8
decode('utf8')将utf8"解码"为unicode

注:linux默认编码为unicode,若为其他两种编码进行转换,需unicode作为“媒介”

如gbk转utf8


阅读资料:

http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html

0 0
原创粉丝点击