xbd

python字符串

点点圈 提交于 2020-02-12 04:41:36
字符串内置函数 ord ( 'A' ) 65 chr ( 88 ) 'X' s1 = 'ABCD' s1 . encode ( 'ASCII' ) b 'ABCD' s2 = '你好啊' s2 . encode ( 'UTF-8' ) b '\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a' s2 . encode ( 'UTF-16' ) b '\xff\xfe`O}YJU' b1 = b '\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a' b1 . decode ( 'UTF-8' ) '你好啊' s1 . encode ( ) #不指定编码形式,默认是utf-8 b 'ABCD' s2 '你好啊' s2 . encode ( ) b '\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a' # 文件编码 open ( 'data.txt' , 'w' , enconding = 'utf8' ) . write ( '你好啊' ) bytes str bytearray 类型转换: bytes bytes ( 'abc' , 'ASCII' ) b 'abc' bytes ( '你好' , 'utf8' ) b '\xe4\xbd\xa0\xe5\xa5\xbd' bytes ( [ 88 , 99 ,