473,395 Members | 1,521 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,395 software developers and data experts.

struct.unpack

Hello Everybody,

I've read a pipe and store it in a object.
My next step was the separation from 4 bytes with
obj = string.join(list(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
and the converting by
value = struct.unpack('I', obj) generated the error
"unpack str size does not match format"

Unfortunately is len(obj) 7, but integer lengt 4.
Why 7 ?

Any ideas ?

gf
Oct 2 '05 #1
6 4496
"g.franzkowiak" wrote:
I've read a pipe and store it in a object.
My next step was the separation from 4 bytes with
obj = string.join(list(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
and the converting by
value = struct.unpack('I', obj) generated the error
"unpack str size does not match format"

Unfortunately is len(obj) 7, but integer lengt 4.
Why 7 ?


because string.join inserts a space between the bytes, by default (as
your example shows)

if you really need that join(list) thing (it's not clear how you read it, but
I find it a bit hard to believe that you've managed to read it into some-
thing that's supported by list but not unpack), you can do

obj = string.join(list(dataObject)[:4], "")

or

obj = "".join(list(dataObject[:4]))

or some variation thereof.

</F>

Oct 2 '05 #2
Fredrik Lundh schrieb:
"g.franzkowiak" wrote:

I've read a pipe and store it in a object.
My next step was the separation from 4 bytes with
obj = string.join(list(dataObject)[:4] ==> '\x16 \x00 \x00 \x00'
and the converting by
value = struct.unpack('I', obj) generated the error
"unpack str size does not match format"

Unfortunately is len(obj) 7, but integer lengt 4.
Why 7 ?

because string.join inserts a space between the bytes, by default (as
your example shows)

if you really need that join(list) thing (it's not clear how you read it, but
I find it a bit hard to believe that you've managed to read it into some-
thing that's supported by list but not unpack), you can do

obj = string.join(list(dataObject)[:4], "")

or

obj = "".join(list(dataObject[:4]))

or some variation thereof.

</F>


I've also found the problem with som tests.
The best of this was:
tmpList = list(dataObject)[:4])
obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].

But your suggestions are much better and new for me - both.

Thanks
gerd
Oct 2 '05 #3
g.franzkowiak wrote:
tmpList = list(dataObject)[:4])
obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].


Have you tried just

obj = dataObject[:4]

without the intermediate list? If that failed, can you tell us the type of
the dataObject? E. g.
print type(dataObject)

<class '__main__.NeitherListNorString'>

Peter

Oct 2 '05 #4
Peter Otten schrieb:
g.franzkowiak wrote:

tmpList = list(dataObject)[:4])
obj = tmpList[0]+tmpList[1]+tmpList[2]+tmpList[3].

Have you tried just

obj = dataObject[:4]

without the intermediate list? If that failed, can you tell us the type of
the dataObject? E. g.

print type(dataObject)


<class '__main__.NeitherListNorString'>

Peter


The dataObject was read from a named pipe as an byte stream

state, dataObject = win32file.ReadFile(handle, nbytes, None)
print repr(dataObject)
==> '\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x0.....

With Frederiks help operates this fine

gerd
Oct 3 '05 #5
g.franzkowiak wrote:
The dataObject was read from a named pipe as an byte stream

state, dataObject = win32file.ReadFile(handle, nbytes, None)
print repr(dataObject)
==> '\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x0.....

With Frederiks help operates this fine
I do not doubt that. My point was that instead of the suggested

# Fredrik
obj*=*"".join(list(dataObject[:4]))

the simpler

# me
obj = dataObject[:4]

might work as well. I cannot test it here, but judging from

http://aspn.activestate.com/ASPN/doc...File_meth.html

the resulting dataObject is a buffer and buffer slices seem to be just
strings.
print repr(dataObject)
==> '\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x0.....


Hmm, that looks as if dataObject were a string -- please post the result of

print type(dataObject)

just to help me restore my peace of mind :-)

Peter

Oct 3 '05 #6
Peter Otten schrieb:
g.franzkowiak wrote:

The dataObject was read from a named pipe as an byte stream

state, dataObject = win32file.ReadFile(handle, nbytes, None)
print repr(dataObject)
==> '\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x0.....

With Frederiks help operates this fine

I do not doubt that. My point was that instead of the suggested

# Fredrik
obj = "".join(list(dataObject[:4]))

the simpler

# me
obj = dataObject[:4]

might work as well. I cannot test it here, but judging from

http://aspn.activestate.com/ASPN/doc...File_meth.html

the resulting dataObject is a buffer and buffer slices seem to be just
strings.

print repr(dataObject)
==> '\x01\x02\x03\x04\x00\x00\x00\x00\x00\x00\x0.....

Hmm, that looks as if dataObject were a string -- please post the result of

print type(dataObject)

just to help me restore my peace of mind :-)

Peter


Hello Peter,

was a node in my mind, the data comes as a string (with readfile always)
<< type(dataObject) = 'str' >> and
<< type(dataObject:4) = 'str' >> also ;-)

I've dropped the loop with list/join and the result is the same.
Thank you :-))

gerd

Oct 3 '05 #7

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

Similar topics

0
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...
5
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:...
5
by: grant | last post by:
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...
16
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...
10
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...
3
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...
4
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...
2
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...
0
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',...
2
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.