Ah, nevermind. I need to be more careful, there isn't an "'as' star_target" after the first rule.
On Thu, Jan 11, 2024 at 10:33 PM Left Right <olegsivo...@gmail.com> wrote: > > 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 > > >> cal...@example.com > > >> > > >> Hobbs > > >> ho...@some.com > > >> > > >> Nancy > > >> na...@herown.com > > >> > > >> Sluggo > > >> slu...@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 -- https://mail.python.org/mailman/listinfo/python-list