
On 5/25/2011 1:29 PM, INADA Naoki wrote:
Sadly, Python 3's bytes is not bytestring.
By intention.
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'.
I presume that you mean you would like to write fin = open(sys.stdin.fileno(), 'r', encoding='bytes') fout = open(sys.stdout.fileno(), 'w', encoding='bytes') If such a thing were added, the 256 bytes should directly map to the first 256 codepoints. I don't know if 'latin1' does that or not. In any case, one can rewrite the above without decoding input lines. with open('tem.py', 'rb') as fin, open('tem2.txt', 'wb') as fout: for n, L in enumerate(fin): fout.write('{0:5d}\t'.format(n).encode('ascii')) fout.write(L) (sys.x.fineno raises fineno AttributeError in IDLE.) -- Terry Jan Reedy