Hi All,
I am pretty new to python and am having a problem
intepreting binary data using struct.unpack.
I am reading a file containing binary packed data
using open with "rb". All the values are coming through
fine when using (integer1,) = struct.unpack('l', line[86:90])
except when line[86:90] contains "carriage-return" "linefeed"
which are valid binary packed values. Error = unpack
string size dows not match format. It seems that
struct, instead of reading 4 bytes for line[86:90]
only reads 2 bytes if the second byte is CR or LF.
Thanks
Grant 5 5378 gr***@idscape.co.za wrote: I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line[86:90]) except when line[86:90] contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that struct, instead of reading 4 bytes for line[86:90] only reads 2 bytes if the second byte is CR or LF.
verifying that struct doesn't care about newlines is of course
pretty trivial: import struct struct.unpack("l", "\0\0\0\0")
(0,) struct.unpack("l", "\0\r\0\0")
(3328,) struct.unpack("l", "\0\n\0\0")
(2560,)
have you verified that len(line) really is what you think?
struct.unpack("l", "\0\r\n\0")
(658688,) struct.unpack("l", "\0\n\0")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
struct.error: unpack str size does not match format
</F>
<gr***@idscape.co.za> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com... except when line[86:90] contains "carriage-return" "linefeed" which are valid binary packed values.
You probably don't want to be reading binary data a
line at a time, if that's what you're doing.
Hi ,
Thanks for the tip regarding checking the length of line. I discovered
that on the problem record it was short by a few bytes. After changing
the read method from "for line in.." to "infile.read(n)" my problem was
solved,
what concerns me though is that although the file is opened in binary
mode,
"for line.." has a problem reading the file correctly.
Thanks
Grant
Fredrik Lundh wrote: gr***@idscape.co.za wrote:
I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line[86:90]) except when line[86:90] contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that struct, instead of reading 4 bytes for line[86:90] only reads 2 bytes if the second byte is CR or LF.
verifying that struct doesn't care about newlines is of course pretty trivial:
import struct struct.unpack("l", "\0\0\0\0") (0,) struct.unpack("l", "\0\r\0\0") (3328,) struct.unpack("l", "\0\n\0\0") (2560,)
have you verified that len(line) really is what you think? struct.unpack("l", "\0\r\n\0") (658688,) struct.unpack("l", "\0\n\0")
Traceback (most recent call last): File "<stdin>", line 1, in ? struct.error: unpack str size does not match format
</F> gr***@idscape.co.za wrote: what concerns me though is that although the file is opened in binary mode, "for line.." has a problem reading the file correctly.
There is _no_ correct way of splitting a file containing binary data in
lines because binary data may contain newline bytes that do not indicate a
new line. E. g. struct.unpack("l", "\r\n\r\n")
(168626701,)
how should Python know whether it just encountered two empty lines or the
integer 168626701?
Peter
Good point. Hadn't thouhgt of that.
Thanks
Grant This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Josiah Carlson |
last post by:
Good day everyone,
I have produced a patch against the latest CVS to add support for two
new formatting characters in the struct module. It is currently an RFE,
which I include a link to at the...
|
by: Geoffrey |
last post by:
Hope someone can help.
I am trying to read data from a file binary file and then unpack the
data into python variables. Some of the data is store like this;
xbuffer:...
|
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure...
|
by: Giovanni Bajo |
last post by:
Hello,
given the ongoing work on struct (which I thought was a dead module), I was
wondering if it would be possible to add an API to register custom parsing
codes for struct. Whenever I use it...
|
by: David Bear |
last post by:
I found this simple recipe for converting a dotted quad ip address to a
string of a long int.
struct.unpack('L',socket.inet_aton(ip))
trouble is when I use this, I get
struct.error: unpack...
|
by: OhKyu Yoon |
last post by:
Hi!
I have a really long binary file that I want to read.
The way I am doing it now is:
for i in xrange(N): # N is about 10,000,000
time = struct.unpack('=HHHH', infile.read(8))
# do...
|
by: brnstrmrs |
last post by:
If I run:
testValue = '\x02\x00'
junk = struct.unpack('h', testValue)
Everything works but If I run
testValue = raw_input("Enter Binary Code..:") inputting at the
console '\x02\x00'
junk...
|
by: Ping Zhao |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine:
header = str(struct.unpack('2s',...
|
by: Heikki Toivonen |
last post by:
M2Crypto has some old code that gets and sets socket timeouts in
http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py,
for example:
def get_socket_read_timeout(self):
return...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |