Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read binary file and dump data in

Reply
Thread Tools

Read binary file and dump data in

 
 
Santiago Romero
Guest
Posts: n/a
 
      01-13-2009

Hi.

Until now, all my python programs worked with text files. But now I'm
porting an small old C program I wrote lot of years ago to python and
I'm having problems with datatypes (I think).

some C code:

fp = fopen( file, "rb");
while !feof(fp)
{
value = fgetc(fp);
printf("%d", value );
}

I started writing:

fp = open(file, "rb")
data = fp.read()
for i in data:
print "%d, " % (int(i))

But it complains about i not being an integer... . len(data) shows
exactly the file size, so maybe is a "type cast" problem...

What's the right way to work with the binary data (read 1 byte values
and work with them, dumping them as an integer in this case)?

Thanks.
 
Reply With Quote
 
 
 
 
Albert Hopkins
Guest
Posts: n/a
 
      01-13-2009
On Tue, 2009-01-13 at 12:02 -0800, Santiago Romero wrote:
> Hi.
>
> Until now, all my python programs worked with text files. But now I'm
> porting an small old C program I wrote lot of years ago to python and
> I'm having problems with datatypes (I think).
>
> some C code:
>
> fp = fopen( file, "rb");
> while !feof(fp)
> {
> value = fgetc(fp);
> printf("%d", value );
> }
>
> I started writing:
>
> fp = open(file, "rb")
> data = fp.read()
> for i in data:
> print "%d, " % (int(i))
>
> But it complains about i not being an integer... . len(data) shows
> exactly the file size, so maybe is a "type cast" problem...
>


int() expects something that "looks like" an integer. E.g.

int(2) => 2
int(2.0) => 2
int('2') => 2
int('c') => ValueError

If you are reading arbitrary bytes then it will likely not always "look"
like integers. What you probably meant is:

for i in data:
print "%d, " % ord(i)

But if you are really dealing with C-like data structures then you might
be better off using the struct module.

-a


 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-13-2009
On Tue, Jan 13, 2009 at 12:02 PM, Santiago Romero <(E-Mail Removed)> wrote:
>
> Hi.
>
> Until now, all my python programs worked with text files. But now I'm
> porting an small old C program I wrote lot of years ago to python and
> I'm having problems with datatypes (I think).
>
> some C code:
>
> fp = fopen( file, "rb");
> while !feof(fp)
> {
> value = fgetc(fp);
> printf("%d", value );
> }
>
> I started writing:
>
> fp = open(file, "rb")
> data = fp.read()
> for i in data:
> print "%d, " % (int(i))
>
> But it complains about i not being an integer... . len(data) shows
> exactly the file size, so maybe is a "type cast" problem...
>
> What's the right way to work with the binary data (read 1 byte values
> and work with them, dumping them as an integer in this case)?


Albert already pointed out the problem with using int(), so I'll just
say that you might be interested in the `struct` module:
http://docs.python.org/library/struct.html

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
Santiago Romero
Guest
Posts: n/a
 
      01-14-2009

> If you are reading arbitrary bytes then it will likely not always "look"
> like integers. What you probably meant is:
>
> for i in data:
> * *print "%d, " % ord(i)


That's it!

Thanks a lot.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dump complete java VM state as core dump (not via OS) possible? halfdog Java 12 02-21-2013 06:14 AM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM
Read Core Dump file ns Cisco 8 05-26-2005 03:07 AM
How do I read binary file data in a test bench? Pete Fraser VHDL 4 11-04-2004 02:51 AM
read binary data from C file??? John Adams Java 15 09-24-2004 06:26 PM



Advertisments