struct.unpack

g.franzkowiak g.franzkowiak at onlinehome.de
Sun Oct 2 17:41:54 EDT 2005


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



More information about the Python-list mailing list