python字符编码与转码

简介:

python 2.x 字符编码与转码


打印系统默认编码格式

1
2
import  sys
print (sys.getdefaultencoding())


UTF-8 转 gbk

方式:utf-8--转成--unicode--转成--gbk

1
2
3
4
5
6
=  "你好"
print  a
utf8_to_unicode  =  a.decode( "utf-8" )                  #utf-8转unicode
print  utf8_to_unicode
unicode_to_gbk  =  utf8_to_unicode.encode( "gbk" )       #unicode转utf-8
print  unicode_to_gbk

gbk 转UTF-8

方式:gbk--转成--unicode--转成--utf-8

1
2
3
4
gbk_to_unicode  =  unicode_to_gbk.decode( "gbk" )        #gbk转unicode
print  gbk_to_unicode
unicode_to_utf8  =  gbk_to_unicode.encode( "utf-8" )     #unicode转uft-8
print  unicode_to_utf8



总结:

    1.python2.X 默认编码是ascii ;支持中文需要加“ #-*- coding:utf-8 ”;

    2.gbk、utf-8之间的转码必须先转码成unicode ;



python 3.x 字符编码与转码


字符编码与转码

UTF-8 可变长编码格式

python3.x 在转码的时候,同时把字符变成一个bytes类型;例如:b'\xc4\xe3\xba\xc3' ;

python3.X 默认的编码是unicode 支持中文;


打印系统默认编码格式

1
2
import  sys
print (sys.getdefaultencoding())


utf-8 转 gbk

由于python3.0 默认unicode 支持中文,所以输入中文,可以直接转gbk ;

1
2
3
4
=  "你好"
print (a)
unicode_to_gbk  =  a.encode( "gbk" )     #unicode 转 gbk
print (unicode_to_gbk)


utf-8 转 gbk      

这里编码转化顺序:unicode--转成--utf-8--转成--unicode--转成--gbk;和上边一样的结果。

1
2
3
4
5
6
7
8
9
10
=  "你好"
print (a)
moren_to_utf8  =  a.encode( "utf-8" )                   
#等于moren_to_utf8 = a.encode()   因为系统编码是utf-8
print ( type (moren_to_utf8.decode( "utf-8" )))          
#utf-8 格式 decode成unicode 就成了一个字符串;
utf8_to_unicdoe  =  moren_to_utf8.decode( "utf-8" )
print (utf8_to_unicdoe)
unicode_to_gbk  =  utf8_to_unicdoe.encode( "gbk" )
print (unicode_to_gbk)



gbk 转 utf-8

1
2
3
4
gbk_to_unicode  =  unicode_to_gbk.decode( "gbk" )        #gbk 转 unicode
print (gbk_to_unicode)
unicode_to_utf8  =  gbk_to_unicode.encode( "utf-8" )     #unicode 转 utf-8
print (unicode_to_utf8)



总结:

    1.python3.x 默认编码是unicode,支持中文;

    2.python3.x 在转码的时候,同时把字符变成一个bytes类型;例如:b'\xc4\xe3\xba\xc3' ;

    3.python3.x 转码成了bytes类型之后,想显示成中文decode成unicode 就成了一个字符串;




     本文转自506554897 51CTO博客,原文链接:http://blog.51cto.com/506554897/1945838,如需转载请自行联系原作者




相关文章
C4.
|
7天前
|
存储 Python
Python字符编码
Python字符编码
C4.
15 1
|
7天前
|
Python
python 二进制与字符编码详解
python 二进制与字符编码详解
12 0
|
7天前
|
存储 编解码 Python
Python中怎么处理字符编码问题
Python中怎么处理字符编码问题
17 0
|
7月前
|
自然语言处理 Java 程序员
Python中的字符串与字符编码2
Hello,这里是Token_w的博客,欢迎您的到来 今天文章讲解的是Python中的字符串与字符编码,其中有基础的理论知识讲解,也有实战中的应用讲解,希望对你有所帮助 整理不易,如对你有所帮助,希望能得到你的点赞、收藏支持。感谢
40 1
|
7月前
|
存储 Java PHP
Python中的字符串与字符编码1
Hello,这里是Token_w的博客,欢迎您的到来 今天文章讲解的是Python中的字符串与字符编码,其中有基础的理论知识讲解,也有实战中的应用讲解,希望对你有所帮助 整理不易,如对你有所帮助,希望能得到你的点赞、收藏支持。感谢
30 1
|
9月前
python--运算符,字符编码
python--运算符,字符编码
|
JSON PHP 数据格式
【python】or【php】网页中字符编码转换,将反斜杠u \u字符串转为unicode/utf8
【python】or【php】网页中字符编码转换,将反斜杠u \u字符串转为unicode/utf8
110 0
|
存储 Java 程序员
PYTHON 字符编码
PYTHON 字符编码
126 0
|
机器学习/深度学习 人工智能 自然语言处理
[oeasy]python0129_unicode_中文字符序号_十三道大辙_字符编码解码_eval_火星文
[oeasy]python0129_unicode_中文字符序号_十三道大辙_字符编码解码_eval_火星文
107 0
[oeasy]python0129_unicode_中文字符序号_十三道大辙_字符编码解码_eval_火星文
|
安全
[oeasy]python0 113_字符编码_VT100控制码_iso_8859_1_拉丁字符_latin
[oeasy]python0 113_字符编码_VT100控制码_iso_8859_1_拉丁字符_latin
115 0
[oeasy]python0 113_字符编码_VT100控制码_iso_8859_1_拉丁字符_latin