Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

19 May, 2024: Line wrapping has been changed to be more consistent with Usenet standards.
 If you find that it is broken please let me know here rocksolid.nodes.help


devel / comp.lang.python / Re: How to write list of integers to file with struct.pack_into?

SubjectAuthor
o How to write list of integers to file with struct.pack_into?Jen Kris

1
Re: How to write list of integers to file with struct.pack_into?

<mailman.388.1696270505.23016.python-list@python.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=24339&group=comp.lang.python#24339

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jenkris@tutanota.com (Jen Kris)
Newsgroups: comp.lang.python
Subject: Re: How to write list of integers to file with struct.pack_into?
Date: Mon, 2 Oct 2023 20:15:03 +0200 (CEST)
Lines: 52
Message-ID: <mailman.388.1696270505.23016.python-list@python.org>
References: <NfhGMY3--3-9@tutanota.com>
<25883.562.500892.142384@ixdm.fritz.box>
<NflaRfs--3-9@tutanota.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de u4o9UN54WRpqsRx9ucousgSKbH+ow2N+LyeOe3KcyULg==
Cancel-Lock: sha1:wdxGoXTA7fCGnpmLe0Am6wv+i1E= sha256:w8fShaN+xzYZTb71yLgaFkGZqaIHQ/zkm0KTik2DtbY=
Return-Path: <jenkris@tutanota.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tutanota.com header.i=@tutanota.com header.b=mkqG9FcL;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.076
X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'argument': 0.04; 'cc:addr
:python-list': 0.09; 'help.\xc2\xa0': 0.09; 'skip:` 10': 0.09;
'skip:` 20': 0.09; 'subject:list': 0.11; 'cc:name:python list':
0.16; 'once.': 0.16; 'writes': 0.16; 'instead': 0.17;
'cc:addr:python.org': 0.20; 'first,': 0.22; 'subject:file': 0.22;
'subject:How': 0.23; 'received:de': 0.23; 'help.': 0.25;
'cc:2**0': 0.25; 'binary': 0.26; 'error': 0.29; 'whole': 0.30;
'code,': 0.31; 'comment': 0.31; 'think': 0.32; 'carefully': 0.32;
'received:192.168.1': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'item': 0.35; 'received:192.168': 0.37; 'read':
0.38; 'thanks': 0.38; 'list': 0.39; 'wrote': 0.39; 'case.': 0.40;
'something': 0.40; 'want': 0.40; 'try': 0.40; 'should': 0.40;
'seen': 0.62; 'skip:b 20': 0.63; 'pass': 0.64; 'research': 0.64;
'skip:r 20': 0.64; 'your': 0.64; 'earlier': 0.67; 'types': 0.67;
'subject:skip:s 10': 0.69; 'signature': 0.76; 'time:': 0.81;
'concluded': 0.91; 'that:': 0.91; 'format:': 0.93
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1696270503;
s=s1; d=tutanota.com;
h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender;
bh=J1QAiPgUs1qH9APqXDYIaUpfc68slkzC26JWKqBnQXw=;
b=mkqG9FcLzYvfYeyNRfJ6vveFImG0jrCNPAgq+W6KjS9B78ffgiccfoTbB89VBzrJ
liV9yQnWX87MwovQJf/Xuy8Fqifkih1lXh6jzOoQ/3Ds/knxo8B/mgsB+7JCkpv2YS/
yHb3y0h8QPf2EuF0uwqnun3yn+DjQgxxCdg6GKLa6YSf2ygqfjYyY+1u76XOhr0sM5Y
Q7B/d9qFdbLGQdEy0ppGCnd1f0sz5K/fy0J5t9AbTvfjg+WakGmsL74e5agp+kP+iKc
+nu5qD+KOICS6Z9nFtjQgaJCAhki2nWY6KRbT3AQI30IidWCMRp86NHTKQDUCg1NgTh
CyytNMSn4A==
In-Reply-To: <25883.562.500892.142384@ixdm.fritz.box>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <NflaRfs--3-9@tutanota.com>
X-Mailman-Original-References: <NfhGMY3--3-9@tutanota.com>
<25883.562.500892.142384@ixdm.fritz.box>
 by: Jen Kris - Mon, 2 Oct 2023 18:15 UTC

Dieter, thanks for your comment that:

* In your code, `offset` is `0`, `1`, `2`, ...
but it should be `0 *8`, `1 * 8`, `2 * 8`, ...

But you concluded with essentially the same solution proposed by MRAB, so that would obviate the need to write item by item because it writes the whole buffer at once. 

Thanks for your help. 

Oct 2, 2023, 17:47 by dieter@handshake.de:

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


devel / comp.lang.python / Re: How to write list of integers to file with struct.pack_into?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor