[Python-ideas] Adding 'bytes' as alias for 'latin_1' codec.

INADA Naoki songofacandy at gmail.com
Wed May 25 19:29:48 CEST 2011


Hi, all.

There are some situation that I want to use bytes as a string in real world.
(I use the 'bstr' for bytes as a string below)

Sadly, Python 3's bytes is not bytestring.
For example, when I want to make 'cat -n' that is transparent to encoding,
Python 3 doesn't permit b'{0:6d}'.format(n) and
'{0:6d}'.format(n).encode('ascii')
is circuitous way against simple requirements.

I think the best way to handle such situation with Python 3 is using 'latin1'
codec. For example, encoding transparent 'cat -n' is:

import sys
fin = open(sys.stdin.fileno(), 'r', encoding='latin1')
fout = open(sys.stdout.fileno(), 'w', encoding='latin1')
for n, L in enumerate(fin):
    fout.write('{0:5d}\t{1}'.format(n, L))

If using 'latin1' is Pythonic way to handle encoding transparent string,
I think Python should provide another alias like 'bytes'.

Any thoughts?

-- 
INADA Naoki  <songofacandy at gmail.com>



More information about the Python-ideas mailing list