在Python中,encodeunicode字符串中有一个方法可以从unicode编码为字节字符串.decode字符串中有一个方法可以反过来.
但我很困惑encode字符串中的方法是什么?
Ign*_*ams 10
它对非文本编解码器很有用.
>>> 'Hello, world!'.encode('hex')
'48656c6c6f2c20776f726c6421'
>>> 'Hello, world!'.encode('base64')
'SGVsbG8sIHdvcmxkIQ==\n'
>>> 'Hello, world!'.encode('zlib')
'x\x9c\xf3H\xcd\xc9\xc9\xd7Q(\xcf/\xcaIQ\x04\x00 ^\x04\x8a'
Run Code Online (Sandbox Code Playgroud)
它首先使用默认编码解码为Unicode,然后编码回字节字符串.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> sys.setdefaultencoding('latin-1')
>>> '\xc4'.encode('utf-8')
'\xc3\x84'
Run Code Online (Sandbox Code Playgroud)
这里,'\xc4'Ä是Latin-1,Ä '\xc3\x84'是UTF-8.
| 归档时间: |
|
| 查看次数: |
2973 次 |
| 最近记录: |