For string objects in Python, it is possible to encode / decode strings with a specific codec, but I will introduce a codec that I did not know much about.
base64
As many of you know this, it encodes / decodes a string into a ** Base64 ** string.
>>> "hello".encode("base64")
'aGVsbG8=\n'
zip
This codec returns the same bytes as if the string was compressed with the ** Zlib ** module.
>>> "hello".encode("zip")
'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'
>>> import zlib
>>> zlib.compress("hello")
'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15' #the same
hex
This codec converts a string to a 2-digit hexadecimal number per byte.
>>> "hello".encode("hex")
'68656c6c6f'
bz2
This codec compresses strings using bz2.
>>> "hello".encode("bz2")
'BZh91AY&SY\x191e=\x00\x00\x00\x81\x00\x02D\xa0\x00!\x9ah3M\x073\x8b\xb9"\x9c(H\x0c\x98\xb2\x9e\x80'
rot13
This codec is [Caesar Cipher](http://en.wikipedia.org/wiki/%E3%82%B7%E3%83%BC%E3%82%B6%E3%83%BC%E6%9A% Encrypt the string using 97% E5% 8F% B7).
>>> "hello".encode("rot_13")
'uryyb'
uu
This codec uses uuencode to convert strings.
>>> print "hello".encode("uu")
begin 666 <data>
%:&5L;&\
end
idna
This codec is a codec that converts ** Unicode character string ** into a character string described in ASCII format on the application from the domain name entered in Kanji etc. specified in RFC3490.
>>> u"Hello".encode("idna")
'xn--mdk0c0b'
There are more special codecs in Python, but I don't use them on a daily basis, so I've only introduced these codecs. zip
and bz2
are very convenient because you can compress strings without using modules.
Recommended Posts