gbk英文字符占几个字节
python常见编码错误集合

python2字符串在内存中如何存放

妙音 posted @ 2018年5月14日 14:07 in python with tags python , 1911 阅读
  • str类型
存在磁盘上的格式与存在内存中格式一致,例如ascii编码占一个字节,utf-8编码中文3个字节,英文1个字节
gbk编码中文2个字节,英文一个字节
 
注意python2是不会自动转换成unicode格式
 
  • unicode类型
python2用来支持多语言,不同编码类型的str转换需要通过它
unicode表示字符串属于逻辑层面,字节串(str)表示存放格式属于物理层面,如ascii,utf-8,gbk属于字节串
 
  • ascii在内存中字节数(utf-8/gbk同样1个字节)
>>> sys.getsizeof("a")       #pyton做了包装,包含了其它数据内容
38
>>> sys.getsizeof("aa") - sys.getsizeof("a")   # 将字符重复一个求差值,可知单个字符长度
1
 
  • unicode在内存中长度(python2中4个字节)
>>> sys.getsizeof(u"aa") - sys.getsizeof(u"a")   #python2中4个字节、python3中2个字节
4
>>> sys.getsizeof(u"你你") - sys.getsizeof(u"你")
4
 
  • utf-8长度(3个字节)
>>> a1,a2="a".encode("utf-8"),"aa".encode("utf-8")
>>> sys.getsizeof(a2) - sys.getsizeof(a1)  #英文一个字节
1

>>> b1,b2="你".decode("utf-8").encode("utf-8"),"你你".decode("utf-8").encode("utf-8")
>>> sys.getsizeof(b2) - sys.getsizeof(b1)   #中文三个字节
3
同样的方法可以知道gbk英文一个字节,中文2个字节
 
 
印光大师十念法(胡小林主讲第1集)

 

Avatar_small
LEGEND SEO 说:
2021年1月07日 17:24

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! 123movie

Avatar_small
LEGEND SEO 说:
2021年1月09日 20:00

nice bLog! its interesting. thank you for sharing.... 메이저사이트


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter