Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Your attitude determines your attitude." -- Zig Ziglar, self-improvement doofus


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

SubjectAuthor
o Re: Extract lines from file, add to new filesThomas Passin

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

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

  copy mid

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

  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: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Extract lines from file, add to new files
Date: Thu, 11 Jan 2024 13:58:07 -0500
Lines: 58
Message-ID: <mailman.10.1705007907.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de EwiS9JSgSipDmMFWNYuSdQTFsVoyzgp34N3BenG3wlPA==
Cancel-Lock: sha1:N1bjsu+t/xz1e8/VdL+OVdL68u8= sha256:04NmVZUOqYTlh/et+ofS4mwRzSZmJJkg1qpzie3GJsg=
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=KKRmBrV/;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'scripts.': 0.07; 'subject:add': 0.07; 'utf-8': 0.07;
'blank': 0.09; "shouldn't": 0.09; 'subject:files': 0.09; 'unsure':
0.09; 'bash': 0.16; 'be,': 0.16; 'email addr:example.com': 0.16;
'empty,': 0.16; 'encoding': 0.16; 'instead.': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'shepard': 0.16; 'wrote:': 0.16;
'python': 0.16; 'addresses': 0.19; 'name.': 0.19; 'pm,': 0.19;
'to:addr:python-list': 0.20; 'input': 0.21; 'written': 0.22;
"i've": 0.22; 'closed': 0.22; 'subject:file': 0.22; 'lines': 0.23;
'header:User-Agent:1': 0.30; 'think': 0.32; 'associate': 0.32;
'python-list': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; "wouldn't": 0.32; "i'm":
0.33; "i'll": 0.33; 'script': 0.33; 'header:In-Reply-To:1': 0.34;
'close': 0.35; 'files': 0.36; '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; 'use': 0.39; 'block': 0.39;
'file:': 0.40; 'files.': 0.40; 'otherwise,': 0.40; 'something':
0.40; 'lack': 0.60; 'brief': 0.62; 'skip:o 20': 0.63; 'ever':
0.63; 'email': 0.63; 'your': 0.64; 'look': 0.65; 'years': 0.65;
'header:Received:6': 0.67; 'received:64': 0.67; 'files:': 0.69;
'it:': 0.69; 'nancy': 0.69; 'experience.': 0.70; 'you.': 0.71;
'ignore': 0.71; 'extracted': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1704999488; a=rsa-sha256;
cv=none;
b=eD6YP8QUqBNWsu9WH2UXFn5BuPkv+rYleO7JWfdgFEPMfW6wdmecViIWwpcDQrGg/4omvs
agIxXqqDwVAVUFsB/b9sBdAo9PE58WhIxnEVSf54SMn6by9OqJ2iRvr5fjCr2Bmmbs8dLL
pok7y9ckRSAQ1xr5QsYEehgOtnpW3Jcxnn++mylqELYJTh4NUz8YY//22fi5iYBpaQpiv2
pRwu0MT1m3bRVtvtW5psd1one6OJXs4ku5BwrMCg4FSRrxc5oQfdgKkO2jOZt/YW1KeWI5
rwwbV76wH5xnbBHryjZLhbKpowseYf2E0n4hmcdVezVQ9+pGjwO1tiz3wGBpGg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1704999488;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=Y7wWEO0oCPrURZNTeO6hg1UeilIGm7ffaLZyAuV/Q4Q=;
b=UQrAqOuzv9GjAUsFRzgL0TZtZpXcjscFAJbafIg4uLSW5usEJ9LqKlGa+ZuE7dQS2vH9dy
OcieppMURowKxYPzRqcGKPo3RbA0+bpeflF/z+FNesc1a6s7xfyId7mY8aVCL7s1Sk7A9l
CJCJlauyc508+GvZxeM0YZi9BaYmx86eI8bcNXEyXKRNlI83yrz8yjWMTLoyqS74BczHTA
Gpha+2PRdfXZgbA/dj0EieNNOdNIfqSfQkL7HaEnXQdBitXb1rx5m9CrIiZOHcbTGi3CR8
nSbH5rmSY3cBRTyiQBLXmlXD9roNUKco2rOFQa7ra0G/p0IMwfIWdxwq0Z65VQ==
ARC-Authentication-Results: i=1; rspamd-568947cb6c-94fr8;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Drop-Gusty: 6e4e05023196027a_1704999488880_667746477
X-MC-Loop-Signature: 1704999488880:1160161992
X-MC-Ingress-Time: 1704999488880
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1704999488;
bh=Y7wWEO0oCPrURZNTeO6hg1UeilIGm7ffaLZyAuV/Q4Q=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=KKRmBrV/ZKbLEvRc7V1XWRIOnX1TcEg/jOtLDF5pqWH2r8BO9XCAD0Ww0HKFlTZ4Q
N3TQhJMn8d+vBnpjImZxTLvUEGeIsg/faKiXDTM4IDuAmeExVKwSQ/EVeCtplgRD9h
ec7DvfbuLtJRW6vMP1Fu52HQmB7JI4apZlIh28aS0UW2abKtV8e0v/1UdYlzcAmF2V
sZvbH5rHnwUgFukyDXd1UvBXY1buU+P97HqCw9IHb+mqpft1MfI1FCji30EEER6dBy
eCx7VpJYR2GhVOfHG4WrmS6/rtSFOZNEtp6kiPQ96Rw2AQsu9Cav+RZCT5pPt2VG+t
WEYFh+Tp9Y5Fg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <24aeb00e-41fd-4809-ae96-d429645cbc07@mrabarnett.plus.com>
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: <a9577a3f-68d6-433b-b87c-4502e2041696@tompassin.net>
X-Mailman-Original-References: <5a6d88e-46b1-a3ea-333-d053cbe5654d@appl-ecosys.com>
<24aeb00e-41fd-4809-ae96-d429645cbc07@mrabarnett.plus.com>
 by: Thomas Passin - Thu, 11 Jan 2024 18:58 UTC

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.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor