A special Python codec that seems to know but does not know

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

A special Python codec that seems to know but does not know
Summary of linux command techniques that I knew when I was a fledgling engineer
A library that seems to be good to know when forced to AJAX non-AJAX WEB services
A special Python codec that seems to know but does not know
How to fix a bug that jupyter notebook does not start automatically
A super beginner who does not know the basics of Python tried to graph the stock price of GAFA
Why does Python have to write a colon?
python Boolean operation does not return a Boolean value
A story that sometimes does not work if pip is up to date
I tried to make a dictionary function that does not distinguish between cases
Key input that does not wait for key input in Python
How to use a tp-link wireless LAN slave unit that does not support Linux
A library that seems to be good to know when forced to AJAX non-AJAX WEB services
[Python] How to write a docstring that conforms to PEP8
[Linux] How to install a package on a server that does not have a network environment (standalone)
Create a defaultdict that returns a defaultdict to create a world where KeyErrror does not occur (+ JSON parsing example)
A story that pyenv is stuck because the python execution command PATH does not pass
A Python script that saves a clipboard (GTK) image to a file.
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
How to write a metaclass that supports both python2 and python3
A python script that converts Oracle Database data to csv
[Python] A story that seemed to fall into a rounding trap
A road to intermediate Python
Python version does not switch
Does python inherit special methods?
I want to specify a file that is not a character string for logrotate, but is it impossible?
When incrementing the value of a key that does not exist
The story that the version of python 3.7.7 was not adapted to Heroku
A Python one-liner that pretty prints json. Do not escape Japanese.
I want to use a wildcard that I want to shell with Python remove
A story that struggled to handle the Python package of PocketSphinx
A record that GAMEBOY could not be done in Python. (PYBOY)
[Python] A memo that I tried to get started with asyncio
A script that returns 0, 1 attached to the first Python prime number
A quick guide to PyFlink that combines Apache Flink and Python
I made a library that adds docstring to a Python stub file.
A super beginner who does not know the basics of Python tried to graph the realized profit and loss data of Rakuten Securities in Python
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left
How to write a Python class
Problem that discord.py does not break
5 Ways to Create a Python Chatbot
Did not change from Python 2 to 3
Introducing a library that was not included in pip on Python / Windows
[Python] I made a decorator that doesn't seem to have any use.
Introducing JustPy, a high-level web framework that does not require front-end programming
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
I made a web application in Python that converts Markdown to HTML
[Python] A program that calculates the number of socks to be paired
A memo that allows you to change Pineapple's Python environment with pyenv
I tried to develop a Formatter that outputs Python logs in JSON
How to use hmmlearn, a Python library that realizes hidden Markov models
Tips: [Python] Extract only lines that do not contain a specific string
A general-purpose tool that programmers who do not know this will lose
[Python] Let's automatically translate English PDF (but not limited to) with DeepL or Google Translate to make a text file.