Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

It's not hard to admit errors that are [only] cosmetically wrong. -- J. K. Galbraith


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

SubjectAuthor
o Re: 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.386.1696268041.23016.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=28514&group=comp.lang.python#28514

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!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 19:33:58 +0200 (CEST)
Lines: 40
Message-ID: <mailman.386.1696268041.23016.python-list@python.org>
References: <NfhGMY3--3-9@tutanota.com>
<b340b761-bb9d-8b16-34ad-a1e5df0b3157@mrabarnett.plus.com>
<NflS2-p--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 g1iNSCb0yWSWeKyEmJaNkAX8kVpJIfvgurVOReiCMogw==
Cancel-Lock: sha1:v1fqvjNgbtiLT/9nooTGFSLN2tk= sha256:33QGOZ0zCjKqWrDkAVN1qT4fivzDRJn0HR6BW8EDwJE=
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=FSvqwmiM;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string': 0.07; 'cc:addr
:python-list': 0.09; 'much,': 0.09; 'subject:list': 0.11;
'url:mailman': 0.15; 'cc:name:python list': 0.16; 'email
addr:python.org:': 0.16; 'frustrated': 0.16; 'integer': 0.16;
'solves': 0.16; 'writes': 0.16; 'wrote:': 0.16; 'python': 0.16;
"can't": 0.17; 'cc:addr:python.org': 0.20; 'subject:file': 0.22;
'subject:How': 0.23; 'received:de': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'tried': 0.26; 'binary': 0.26; 'object': 0.26; 'output': 0.28;
'this?': 0.29; 'error': 0.29; 'url-ip:188/8': 0.31; 'think': 0.32;
'python-list': 0.32; 'received:192.168.1': 0.32; 'but': 0.32;
"i'm": 0.33; 'requires': 0.34; 'header:In-Reply-To:1': 0.34;
'those': 0.36; 'using': 0.37; "it's": 0.37; 'received:192.168':
0.37; 'way': 0.38; 'thanks': 0.38; 'list': 0.39; 'wrote': 0.39;
'difficult': 0.40; 'program.': 0.40; 'want': 0.40; 'seen': 0.62;
'skip:b 20': 0.63; 'research': 0.64; 'skip:r 20': 0.64; 'pack':
0.64; 'your': 0.64; 'earlier': 0.67; 'subject:skip:s 10': 0.69;
'extra': 0.84
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1696268038;
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=y1Hk0QAvNUDCJBDujMD/jF/flZIxdhPngV2AdFlACi0=;
b=FSvqwmiMRzLXt+0S72w9r8PkEcD6Y0D6nX2ib9jqB45b5pth15fGIuVD0pmfpzcy
PBQ0iVJA+wh7e/6zS2C18Pp22yXUBENHTu3nt5TOr2mN1qJ42j7YkjS1K2RZmJj+BHB
jnB9AoIZY/eGaGrkBbjEPziCgug1LoPVrz0XYCYzxulAJYvcodJ9dqxepRpL8L0hUQ7
45XxHf2lgyt/f5veCXl9/8R5hnEUrK+hl00dMQWg2PUE2VZQ2ko0tg3dzoweVJN593Z
QS6js+LW5o9i0GHOp+JCw4TK5bJFHoGyu0XnOLtgW3BwmfjiNVcxkVnnDxFoGyacQBs
J/DySLbB7w==
In-Reply-To: <b340b761-bb9d-8b16-34ad-a1e5df0b3157@mrabarnett.plus.com>
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: <NflS2-p--3-9@tutanota.com>
X-Mailman-Original-References: <NfhGMY3--3-9@tutanota.com>
<b340b761-bb9d-8b16-34ad-a1e5df0b3157@mrabarnett.plus.com>
 by: Jen Kris - Mon, 2 Oct 2023 17:33 UTC

Thanks very much, MRAB.  I just tried that and it works.  What frustrated me is that every research example I found writes integers as strings.  That works -- sort of -- but it requires re-casting each string to integer when reading the file.  If I'm doing binary work I don't want the extra overhead, and it's more difficult yet if I'm using the Python integer output in a C program.  Your solution solves those problems. 

Oct 2, 2023, 17:11 by python-list@python.org:

> On 2023-10-01 23:04, Jen Kris via Python-list wrote:
>
>>
>> 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?
>>
> You can't pack into a 'bytes' object because it's immutable.
>
> 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
>


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