字符串

字符串的类型

●bytes: 二进制

互联网.上数据的都是以二进制的方式传输的

str : unicode的呈现形式

字符(Cheracter)是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字

字符集(Character set)是多个字符的集合

字符集包括: ASCII字符集、GB2312字符集、GB18030字符集、Unicode字符集等

ASCI编码是1个字节,而Unicode编码通常是2个字节。

UTF-8是Unicode的实现方式之一,UTF-8是它是一 种变长的编码方式,可以是1, 2, 3个字节

 

 

str bytes如何转化
●str 使用encode方法转化为bytes
● bytes通过decode转化为str
●编码方式解码方式必须一样,否则就会出现乱码

a = "scy孙创昱"
print( a,type(a) )
a = a.encode()#默认编码方式是utf-8
print( a,type(a) )
print(a.decode())
print(a.decode("gbk"))

scy孙创昱 <class 'str'>
b'scy\xe5\xad\x99\xe5\x88\x9b\xe6\x98\xb1' <class 'bytes'>
scy孙创昱
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled/test.py", line 6, in <module>
print(a.decode("gbk"))
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb1 in position 11: incomplete multibyte sequence

 

 

.

posted @ 2020-07-03 06:40  SunCY  阅读(141)  评论(0编辑  收藏  举报