Reputation: 11
I'm trying to use the struct.pack so I can write a string into a file. When I do, I get the following error:
File "----", line 166, in main
struct.pack('>256s', *master_header)
struct.error: pack expected 1 items for packing (got 256)
Now, reading from here I seem to be using it right. I specify that I'm getting 256 bytes/characters in my string.
I'm using version 3.3.3.
Upvotes: 1
Views: 3630
Reputation: 154836
The documentation says:
For the 's' format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters.
So, >256s
expects a single string 256 bytes long. If master_header
already is such a string, just pass it to struct.pack
without the *
.
Using the *
at the call site causes the string itself to be unpacked into its constituent characters, strings being iterable. As a result, struct.pack
receives its 256 individual characters as arguments, causing the observed error.
Upvotes: 1