python编码
2021-02-13 06:20
                         标签:body   http   rac   python编码   line   detail   border   ror   python3    参考文章:https://blog.csdn.net/yanghuan313/article/details/63262477   python编码:encode() 将Unicode字符按照编码规则(如UTF-8)编成字节序列。 >>> a = u"测试" python解码:decode() 将字节序列按照编码规则(如UTF-8)解释成unicode。 >>> a = b"测试"   Unicode(文本字符串) bytes(字节字符串) str 对象有一个encode方法,bytes 对象有一个decode方法     # python3 >>> a = "测试"  # b‘‘表示bytes(二进制)类型;str表示unicode(字符)类型   python编码 标签:body   http   rac   python编码   line   detail   border   ror   python3    原文地址:https://www.cnblogs.com/wangsl1204/p/10374197.html
>>> a.encode("UTF-8")
‘\xe6\xb5\x8b\xe8\xaf\x95‘
>>> a.decode("GBK")
u‘\u5a34\u5b2d\u762f‘
 
  
默认编码(str) 
  
总结 
 
python2 
 bytes(字节字符串)
 
 unicode(文本字符串) 
str 对象有一个encode方法,bytes 对象有一个decode方法,str有个encode()方法,unicode有个decode()方法,但永远不要使用它们。 
 
python3 
 
 
 
# python2
>>> a = "测试"
>>> type(a)
>>> type(a)
>>> a.encode("UTF-8")
b‘\xe6\xb5\x8b\xe8\xaf\x95‘
>>> type(a.encode("UTF-8"))
>>> 
>>> b = b"测试"
  File "
SyntaxError: bytes can only contain ASCII literal characters.
>>> b = b"wangsl"
>>> type(b)
>>> b.decode("UTF-8")
‘wangsl‘
>>> type(b.decode("UTF-8"))
# 如下是互相转换方式
b‘‘ = str.encode(‘utf-8‘)
str = b‘‘.decode(‘utf-8‘)
上一篇:C++模板