-
四个阶段
爬虫爬数据、数据库、后台、前端编码必须均一致、正确
GBK区位码:针对汉字
utf-8和utf-16区别是大小区别,即存储字库的内容数量区别ascii->gbk(2个字节)->unicode编码格式(4个字节)->utf-8是一种可变类型(3个字节,英文一个中文三个)
-
encode()和decode()里面格式必须一致,比如全为uts-8
-
encode()将字符串编码为字节码,0或1,不会乱码
word='hello python,你好 中国'#word为utf-8 byte_gbk_word=word.encode(encoding='GBK')#两个字节一个中文 byte_utf_word=word.encode(encoding='UTF-8')#三个字节一个中文 print(byte_gbk_word) print(byte_utf_word)
-
decode()解码,将字节码解码为字符串
str_gbk_word=byte_gbk_word.decode(encoding='GBK') str_utf_word=byte_utf_word.decode(encoding='UTF-8') print(str_gbk_word)#word已经转成GBK print(str_utf_word)#word已经转成UTF-8
Python-6-字符串的编码和解码
最新推荐文章于 2024-02-18 10:29:54 发布