首页 > 编程语言 > 详细

python中编码问题

时间:2019-03-23 18:50:32      阅读:168      评论:0      收藏:0      [点我收藏+]

python3代码执行过程:

  1. 解释器找到代码文件(文件以utf8/GBK..存储),
  2. 把代码字符串按文件头定义的编码进行解码到内存,转成unicode
  3. 所有的变量字符都会以unicode编码声明(str的编码方式就是unicode)

 

unicode只在内存中进行显示, 传输和存储需要用到utf8/GBK.., 所以必须转成utf8/GBK..

 

 

 str和bytes的区别就是编码方式的不同:

 

1 str(unicode编码)      ==>     bytes(utf8/GBK..)       ==>         存储, 传输
2 bytes = str.encode(utf-8)     # 编码
3 str = bytes.decode(utf-8)     # 解码

 

 

 

python3中str和bytes表现和编码:

 1 英文:
 2     str:    表现方式==>a
 3             编码方式==>0101      unicode
 4 
 5     bytes:  表现方式==>ba
 6             编码方式==>0101      utf8/GBK..
 7 
 8 
 9 中文:
10     str:    表现方式==>
11             编码方式==>0101      unicode
12 
13     bytes:  表现方式==>bx\e9
14             编码方式==>0101      utf8/GBK..

 

 

在python2中:

  1. u‘xxx‘为unicode对象, 就是python3中的str
  2. bytes和str是同一个类型

 

 

 1 s = a
 2 print (s, type(s))              # ‘a‘, <type ‘str‘>
 3 
 4 
 5 s = u中文
 6 print(s, type(s))               # u‘\u4e2d\u6587‘, <type ‘unicode‘>
 7 # 编码变成utf-8, 一个中文三个字节
 8 s1 = s.encode(utf-8)
 9 print(s1, type(s1))             # ‘\xe4\xb8\xad\xe6\x96\x87‘, <type ‘str‘>
10 
11 
12 # bytes和str是同一个类型
13 s1 = a
14 s2 = bytes(a)
15 print(s1 is s2)                 # True

 

python中编码问题

原文:https://www.cnblogs.com/caihuajiaoshou/p/10585032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!