Newbie - converting binary data to ASCII text ?

Emile van Sebille emile at fenx.com
Sat Oct 13 10:07:29 EDT 2001


"Cyr" <cd.cd at wanadoo.fr> wrote in message news:9q92g4$6hd$1 at wanadoo.fr...
> hello,
>
> I'm completely new to python and I've got problem reading binary data
file.
> When I read a file with read(), the binary data aren't converted. In fact,
> I'd like to know if there's a method for converting a file object (or a
> string) from binary to the equivalent ASCII text. The data aren't written
in
> a particular format. Fortran doesn't have any problem with theese files
>
> I'would greetly appreciate any help, thanks
>
> Cyril D
>
> PS : I'm working on an SGI Irix operating system
>
>

Getting the data shouldn't be a problem.

>>> bytes = open("command.com",'rb').read()
>>> len(bytes)
93880    # which matches
>>> print bytes[:10]
MZ? ?   $?
>>> print repr(bytes[:10])
'MZ\xb8\x00\xb8\x00\x00\x00$\x04'

As for making sense of the bytes, the only way you'll do that is to
use/write a parser that understands how the content was put on the disk, and
each application pretty much decides its own internal data scheme.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list