File encoding strategy question

John Machin sjmachin at lexicon.net
Sun May 21 09:14:14 EDT 2006


Andrew > I would like to be able to view the contents of the file if it
is text
while still maintaining the ability to transmit binary data.

Like Dennis said ... and once you have read the RFC and understood it
thoroughly :-) don't start writing code; it's one of the included
batteries -- but beware of file size expansion for binary data:

>>> import quopri
>>> a = ''.join(chr(x) for x in range(256))
>>> b = quopri.encodestring(a)
>>> c = quopri.decodestring(b)
>>> [len(x) for x in a,b,c]
[256, 530, 256]
>>> a == c
True
>>> b
'\x00\x01\x02\x03\x04\x05\x06\x07\x08=09\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14
\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
!"#$%&\'()*+,-./0123456789:;<=3D>?@
ABCDEFGHIJKLMNOPQRS=\nTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~=7F=80=81=82=8
3=84=85=86=87=88=\n=89=8A=8B=8C=8D=8E=8F=90=91=92=93=94=95=96=97=98=99=9A=9B=9C=
9D=9E=9F=A0=A1=\n=A2=A3=A4=A5=A6=A7=A8=A9=AA=AB=AC=AD=AE=AF=B0=B1=B2=B3=B4=B5=B6
=B7=B8=B9=BA=\n=BB=BC=BD=BE=BF=C0=C1=C2=C3=C4=C5=C6=C7=C8=C9=CA=CB=CC=CD=CE=CF=D
0=D1=D2=D3=\n=D4=D5=D6=D7=D8=D9=DA=DB=DC=DD=DE=DF=E0=E1=E2=E3=E4=E5=E6=E7=E8=E9=
EA=EB=EC=\n=ED=EE=EF=F0=F1=F2=F3=F4=F5=F6=F7=F8=F9=FA=FB=FC=FD=FE=FF'
>>>

HTH,
John




More information about the Python-list mailing list