473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with struct module


I'm trying to use the unpack method in the struct module to parse a
binary file without success. I have a binary file with records that
include many fields for a total length of 1970. Few days ago I was
suggested by the list to use the struct module to parse it using the
following code in the hypothesis that for each records I have just two
fields:

import struct
fmt='10s1960s'
size=struct.calcsize(fmt)
f=file("data")
s=f.read(size)

while s:
print 'read:', s
d1,d2=struct.unpack(fmt,s)
print 'unpack', d1

Probably I was not even able to explain my problem since it seem that
this code is not able to read the file. Can anybody help me, maybe
asking for other infos they need? Also just suggest me a place where I
can find examples of using struc.unpack would be very helpful.
Thanks.
a.


--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======

Jul 18 '05 #1
2 2034
Angelo,

Two things:

1) To open a file for reading binary,
I think you meant to write:

f=open("data","rb")

2) You don't need struct to decode a 10 character
string followed by a 1960 character string (10s1960s).
If they are really strings, just read them normally.
If they are binary data, then you should decode them
using struct, but you wouldn't use 10s1960s as format
you would use binary constructs in the format.

Perhaps more detail on what you "think" the structure
of the 1970 byte "records" is would help us help you.

-Larry
"Angelo Secchi" <se****@sssup.it> wrote in message
news:ma************************************@python .org...

I'm trying to use the unpack method in the struct module to parse a
binary file without success. I have a binary file with records that
include many fields for a total length of 1970. Few days ago I was
suggested by the list to use the struct module to parse it using the
following code in the hypothesis that for each records I have just two
fields:

import struct
fmt='10s1960s'
size=struct.calcsize(fmt)
f=file("data")
s=f.read(size)

while s:
print 'read:', s
d1,d2=struct.unpack(fmt,s)
print 'unpack', d1

Probably I was not even able to explain my problem since it seem that
this code is not able to read the file. Can anybody help me, maybe
asking for other infos they need? Also just suggest me a place where I
can find examples of using struc.unpack would be very helpful.
Thanks.
a.


--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======

Jul 18 '05 #2
Angelo Secchi <se****@sssup.it> wrote in message news:<ma************************************@pytho n.org>...
I'm trying to use the unpack method in the struct module to parse a
binary file without success. I have a binary file with records that


You did not mention what error you got, but I can guess that
it is because you are trying to unpack the structure inside an
infinite loop.

import struct
fmt='10s1960s'
size=struct.calcsize(fmt)
f=file("data")
s=f.read(size)
while s:
print 'read:', s
d1,d2=struct.unpack(fmt,s)
print 'unpack', d1
s = f.read (size) ### YOU FORGOT THIS LINE.

Yours,
Noah
Spurrier
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt Feinstein | last post by:
Using the 'struct' module (Win32, python version 2.4.1)-- The library documentation says that 'no alignment is required for any type'. However, struct.calcsize('fd') gives 16 while...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
1
by: andychambers2002 | last post by:
Hi All, A C library I'm using has a number of functions that all require a struct as an argument. The example module shows how to make a new Python Object from C code and I've seen other posts...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible...
0
by: Keith | last post by:
Hello, I am trying to create exectuables on inux using "pyinstaller". I am using pyinstaller-1.3, RHEL 4.4, Python 2.5. The executables fail to run. The problem returned is pertaining to...
0
by: abarun22 | last post by:
Hi I am facing a problem while including a C header file in the SWIG interface file. However the problem does not occur when i directly copy the contents of header file in the same place. My...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
8
by: Bryan.Fodness | last post by:
Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. Here is my function that takes in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.