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: Extract lines from file, add to new files

SubjectAuthor
o Re: Extract lines from file, add to new filesLeft Right

1
Re: Extract lines from file, add to new files

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: olegsivokon@gmail.com (Left Right)
Newsgroups: comp.lang.python
Subject: Re: Extract lines from file, add to new files
Date: Thu, 11 Jan 2024 22:33:27 +0100
Lines: 104
Message-ID: <mailman.11.1705010119.15798.python-list@python.org>
References: <5a6d88e-46b1-a3ea-333-d053cbe5654d@appl-ecosys.com>
<24aeb00e-41fd-4809-ae96-d429645cbc07@mrabarnett.plus.com>
<a9577a3f-68d6-433b-b87c-4502e2041696@tompassin.net>
<CAJQBtgnNHtX+16LEyCh-GQATCR0Yp-7zaKJaG3y-gyh7+nZJAg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de jZXO1oSd53TLVObbrgosPgfeg6lVRSyjo+c7P09nAZKQ==
Cancel-Lock: sha1:7xOxad1QFFWFEpTLOe84S2Q5RRw= sha256:gk+RxBBCeSN/s30h+xEOX6IGVD66gqPyLHVlDD8ollc=
Return-Path: <olegsivokon@gmail.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=gmail.com header.i=@gmail.com header.b=LpD2DGeI;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'traceback': 0.04; '(most': 0.05; 'last):': 0.05;
'scripts.': 0.07; 'subject:add': 0.07; 'utf-8': 0.07; 'blank':
0.09; 'cc:addr:python-list': 0.09; 'expression': 0.09; 'parse':
0.09; "shouldn't": 0.09; 'subject:files': 0.09; 'typeerror:':
0.09; 'unsure': 0.09; 'cc:no real name:2**0': 0.14; 'url:mailman':
0.15; '2024': 0.16; 'bash': 0.16; 'be,': 0.16; 'email
addr:example.com': 0.16; 'empty,': 0.16; 'encoding': 0.16;
'instead.': 0.16; 'received:mail-ua1-x92e.google.com': 0.16;
'shepard': 0.16; 'which,': 0.16; 'wrote:': 0.16; 'python': 0.16;
'addresses': 0.19; 'name.': 0.19; 'pm,': 0.19; 'thu,': 0.19;
'cc:addr:python.org': 0.20; 'input': 0.21; 'written': 0.22;
"i've": 0.22; 'closed': 0.22; 'problem,': 0.22; 'subject:file':
0.22; 'lines': 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; 'seems': 0.26; '11,': 0.26; 'object':
0.26; 'else': 0.27; 'attempt': 0.31; 'url-ip:188/8': 0.31;
'think': 0.32; 'associate': 0.32; 'context': 0.32; 'python-list':
0.32; "wouldn't": 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; "i'm": 0.33; "i'll": 0.33; 'script': 0.33; 'header:In-Reply-
To:1': 0.34; 'received:google.com': 0.34; 'running': 0.34;
'close': 0.35; 'from:addr:gmail.com': 0.35; 'files': 0.36; "skip:'
10": 0.37; 'subject:from': 0.37; 'using': 0.37; "it's": 0.37;
'file': 0.38; 'way': 0.38; 'could': 0.38; 'read': 0.38; 'two':
0.39; 'single': 0.39; 'this,': 0.39; 'use': 0.39; 'block': 0.39;
'file:': 0.40; 'files.': 0.40; 'otherwise,': 0.40; 'something':
0.40; 'lack': 0.60; 'skip:o 10': 0.61; 'brief': 0.62; 'skip:o 20':
0.63; 'ever': 0.63; 'email': 0.63; 'your': 0.64; 'look': 0.65;
'years': 0.65; 'manager': 0.68; 'files:': 0.69; 'interpreted':
0.69; 'it:': 0.69; 'nancy': 0.69; 'surprise,': 0.69;
'experience.': 0.70; 'you.': 0.71; 'ignore': 0.71; 'discovered':
0.80; 'decoration.': 0.84; 'extracted': 0.84; 'golf': 0.84;
'skip:" 40': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1705008819; x=1705613619; darn=python.org;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=rmJf4rd4wMhIl4vHGXXI7+PbW/JKD64tuc7uGaTQaYg=;
b=LpD2DGeIqz++Od+WUv8Y8fdbNpn+9+OUja277BQWq0fJZtCJc60mkSPCGfgbhu2CtA
yfRjM9QjxtfLKJ+sye/EJo29rc5BLl+/pGu91nchfVHhMnqy/q6hYrtZmSkBbLqz0jfZ
wqu43RS3SuKG44LMrC5VH4NNIT7f//nl87P8gWqmYqNmiv6dqCasbKyOPusnF8vsbkO1
II/F6R6Q/UhPp9x/w6GKyLcl2eNyqteJeTxhNlv8aro9O5ww9vEEE0DbmAdYLTVS6dXb
PYPOQX+iNzr/dSF5lGwVf9gB4efGTyvVBeb1cXTYOBpO5R5htm+sOe2aKPxrbQE0M8i8
jLTA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1705008819; x=1705613619;
h=content-transfer-encoding:cc:to:subject:message-id:date:from
:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=rmJf4rd4wMhIl4vHGXXI7+PbW/JKD64tuc7uGaTQaYg=;
b=HBmVp2XZbzFEFg3EA81Z5GsBlbO0NnMHYFBPvAnHskleeR2zjIesuWAgQ6kTZIW8Ev
9LAUZH9EHP5koGAkehq4f8hwbZvMR9T6V0ByvZO7nRkb/iqJy3LtartGT1I0X4TpiV88
ppkDqvKMRkORsoNadajrI63k/SYg6/nAIBDC1R6BXJj0O0wGXo5outnF+DVvXW4y16fS
xhn29JS3bYk6Iz8dbCSAPrKDKNRBU+AOPKYNViqxorhjrlQZJcuQSSwuTCx7hO9plA/K
CXoF0NMYFlpvJoOIRbcddFGpjrSnvz/HmfJfMvJSEvuE8yRZoeiiGehDaSQujamYM//o
jGPg==
X-Gm-Message-State: AOJu0YxqK6pU3F2irYQ6l3hIa/r3aUOpTh05+zRjgVgOZC4e8feZTPGk
0QyGUiwlckpGNAZcqrB7XYGMIvH64ix9D3xXMxKVXPS6ZeM=
X-Google-Smtp-Source: AGHT+IEPIzZLonDFQto3DaSO31IrZYQoV+1WkrrsZzAft184seilES8xdEPzEOxbuwhcaoOB+00sDA75tOUZOtekCiY=
X-Received: by 2002:a05:6102:559a:b0:467:2737:4d52 with SMTP id
dc26-20020a056102559a00b0046727374d52mr548070vsb.24.1705008819028; Thu, 11
Jan 2024 13:33:39 -0800 (PST)
In-Reply-To: <a9577a3f-68d6-433b-b87c-4502e2041696@tompassin.net>
X-Mailman-Approved-At: Thu, 11 Jan 2024 16:55:18 -0500
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: <CAJQBtgnNHtX+16LEyCh-GQATCR0Yp-7zaKJaG3y-gyh7+nZJAg@mail.gmail.com>
X-Mailman-Original-References: <5a6d88e-46b1-a3ea-333-d053cbe5654d@appl-ecosys.com>
<24aeb00e-41fd-4809-ae96-d429645cbc07@mrabarnett.plus.com>
<a9577a3f-68d6-433b-b87c-4502e2041696@tompassin.net>
 by: Left Right - Thu, 11 Jan 2024 21:33 UTC

By the way, in an attempt to golf this problem, I discovered this,
which seems like a parser problem:

This is what Python tells me about its grammar:

with_stmt:
| 'with' '(' ','.with_item+ ','? ')' ':' block
| 'with' ','.with_item+ ':' [TYPE_COMMENT] block
| ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block
| ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block

with_item:
| expression 'as' star_target &(',' | ')' | ':')
| expression

>From which I figured why not something like this:

with (open('example.txt', 'r'), open('emails.txt', 'w'),
open('salutations.txt', 'w')) as e, m, s:
for line in e:
if line.strip():
(m if '@' in line else s).write(line)

Which, surprise, parsers! But it seems like it's parse is wrong,
because running this I get:

❯ python ./split_emails.py
Traceback (most recent call last):
File "/home/?/doodles/python/./split_emails.py", line 1, in <module>
with (open('example.txt', 'r'), open('emails.txt', 'w'),
open('salutations.txt', 'w')) as e, m, s:
TypeError: 'tuple' object does not support the context manager protocol

It seems to me it shouldn't have been parsed as a tuple. The
parenthesis should've been interpreted just as a decoration.

NB. I'm using 3.11.6.

On Thu, Jan 11, 2024 at 10:20 PM Thomas Passin via Python-list
<python-list@python.org> wrote:
>
> On 1/11/2024 1:27 PM, MRAB via Python-list wrote:
> > On 2024-01-11 18:08, Rich Shepard via Python-list wrote:
> >> It's been several years since I've needed to write a python script so I'm
> >> asking for advice to get me started with a brief script to separate names
> >> and email addresses in one file into two separate files:
> >> salutation.txt and
> >> emails.txt.
> >>
> >> An example of the input file:
> >>
> >> Calvin
> >> calvin@example.com
> >>
> >> Hobbs
> >> hobbs@some.com
> >>
> >> Nancy
> >> nancy@herown.com
> >>
> >> Sluggo
> >> sluggo@another.com
> >>
> >> Having extracted salutations and addresses I'll write a bash script using
> >> sed and mailx to associate a message file with each name and email
> >> address.
> >>
> >> I'm unsure where to start given my lack of recent experience.
> >>
> > From the look of it:
> >
> > 1. If the line is empty, ignore it.
> >
> > 2. If the line contains "@", it's an email address.
> >
> > 3. Otherwise, it's a name.
>
> You could think about a single Python script that looks through your
> input file and constructs all the message files without ever writing
> separate salutation and address files at all. Then you wouldn't need to
> write the sed and mailx scripts. It shouldn't be much harder than
> peeling out the names and addresses into separate files.
>
> If you haven't written any Python for some years, the preferred way to
> read and write files is using a "with" statement, like this:
>
> with open('email_file.txt', encoding = 'utf-8') as f:
> lines = f.readlines()
> for line in lines:
> if not line.strip(): # Skip blank lines
> continue
> # Do something with this line
>
> You don't need to close the file because when the "with" block ends the
> file will be closed for you.
>
> If the encoding is not utf-8 and you know what it will be, use that
> encoding instead.
>
> --
> https://mail.python.org/mailman/listinfo/python-list


devel / comp.lang.python / Re: Extract lines from file, add to new files

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor