By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,365 Members | 1,715 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,365 IT Pros & Developers. It's quick & easy.

struct.unpack

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.