struct.unpack

G

g.franzkowiak

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
 
F

Fredrik Lundh

g.franzkowiak said:
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>
 
G

g.franzkowiak

Fredrik said:
:

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
 
P

Peter Otten

g.franzkowiak said:
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.
<class '__main__.NeitherListNorString'>

Peter
 
G

g.franzkowiak

Peter said:
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.


<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
 
P

Peter Otten

g.franzkowiak said:
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/docs/ActivePython/2.3/pywin32/win32file__ReadFile_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
 
G

g.franzkowiak

Peter said:
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/docs/ActivePython/2.3/pywin32/win32file__ReadFile_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top