Iwant to write a list of 64-bit integers to a binary file.Everyexample I have seen in my research convertsit to .txt, but I want
buf= bytes((len(qs_array)) * 8)
foroffset in range(len(qs_array)):
item_to_write= bytes(qs_array[offset])
struct.pack_into(buf,"<Q", offset, item_to_write)
ButI get the error "struct.error: embedded null character."
Maybethere's a better way to do this?
On 2023-10-01 23:04, Jen Kris via Python-list wrote:
You can't pack into a 'bytes' object because it's immutable.
Iwant to write a list of 64-bit integers to a binary file. Everyexample I have seen in my research convertsit to .txt, but I want it in binary. I wrote this code,based on some earlier work I have done:
buf= bytes((len(qs_array)) * 8)
foroffset in range(len(qs_array)):
item_to_write= bytes(qs_array[offset])
struct.pack_into(buf,"<Q", offset, item_to_write)
ButI get the error "struct.error: embedded null character."
Maybethere's a better way to do this?
The simplest solution I can think of is:
buf = struct.pack("<%sQ" % len(qs_array), *qs_array)
--
https://mail.python.org/mailman/listinfo/python-list
Iwant to write a list of 64-bit integers to a binary file. Everyexample I have seen in my research convertsit to .txt, but I want it in binary. I wrote this code,based on some earlier work I have done:
buf= bytes((len(qs_array)) * 8)
for offset in range(len(qs_array)):
item_to_write= bytes(qs_array[offset])
struct.pack_into(buf,"<Q", offset, item_to_write)
But I get the error "struct.error: embedded null character."
Jen Kris wrote at 2023-10-2 00:04 +0200:
Iwant to write a list of 64-bit integers to a binary file. Everyexample I have seen in my research convertsit to .txt, but I want it in binary. I wrote this code,based on some earlier work I have done:
buf= bytes((len(qs_array)) * 8)
for offset in range(len(qs_array)):
item_to_write= bytes(qs_array[offset])
struct.pack_into(buf,"<Q", offset, item_to_write)
But I get the error "struct.error: embedded null character."
You made a lot of errors:
* the signature of `struct.pack_into` is
`(format, buffer, offset, v1, v2, ...)`.
Especially: `format` is the first, `buffer` the second argument
* In your code, `offset` is `0`, `1`, `2`, ...
but it should be `0 *8`, `1 * 8`, `2 * 8`, ...
* The `vi` should be something which fits with the format:
integers in your case. But you pass bytes.
Try `struct.pack_into("<Q", buf, 0, *qs_array)`
instead of your loop.
Next time: carefully read the documentation and think carefully
about the types involved.
My previous message just went up -- sorry for the mangled formatting. Here it is properly formatted:
I want to write a list of 64-bit integers to a binary file. Every example I have seen in my research converts it to .txt, but I want it in binary. I wrote this code, based on some earlier work I have done:
buf = bytes((len(qs_array)) * 8)
for offset in range(len(qs_array)):
item_to_write = bytes(qs_array[offset])
struct.pack_into(buf, "<Q", offset, item_to_write)
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 14:23:27 |
| Calls: | 12,101 |
| Calls today: | 1 |
| Files: | 15,004 |
| Messages: | 6,518,022 |