Extract lines from file, add to new files
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. TIA, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Thu, 11 Jan 2024, MRAB via Python-list wrote: 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. MRAB, Thanks. I'll take it from here. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On 1/11/24 11:27, 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. 4. Don't assume it's going to be "plain text" if the email info is harvested from external sources (like incoming emails) - you'll end up stumbling over a 誰かのユーザー from somewhere. Process as bytes, or be really careful about which encodings you allow - which for email "names" is something you can't actually control. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Thu, 11 Jan 2024, Mats Wichmann via Python-list wrote: 4. Don't assume it's going to be "plain text" if the email info is harvested from external sources (like incoming emails) - you'll end up stumbling over a 誰かのユーザー from somewhere. Process as bytes, or be really careful about which encodings you allow - which for email "names" is something you can't actually control. Mats, Not an issue for me. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On 11/01/2024 19.08, Rich Shepard 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. Why not to use bash script for all? bye, -- piergiorgio -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Thu, 11 Jan 2024, Piergiorgio Sartor via Python-list wrote: Why not to use bash script for all? Piergiorgio, That's certainly a possibility, and may well be better than python for this task. Thank you, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
Am 11.01.24 um 20:53 schrieb Rich Shepard via Python-list: On Thu, 11 Jan 2024, Piergiorgio Sartor via Python-list wrote: Why not to use bash script for all? Piergiorgio, That's certainly a possibility, and may well be better than python for this task. Thank you, Rich awk '/@/ {print >>"emails.txt";next};NF{print >>"salutation.txt"}' input.txt SCNR ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
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
Re: Extract lines from file, add to new files
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 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 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
Re: Extract lines from file, add to new files
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 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 > 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 > 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
Re: Extract lines from file, add to new files
On 12/01/24 10:33, Left Right via Python-list 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 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. A series of comma-separated items will be parsed as a tuple (some people think it is bounding-parentheses which define). In this case, the issue is 'connecting' the context-manager "expression" with its (as) "target". These should be more-closely paired:- with ( open( 'example.txt', 'r', ) as e, open( 'emails.txt', 'w', ) as m, open( 'salutations.txt', 'w', ) as s ): (NB code not executed here) A data-architecture of having related-data in separated serial-files is NOT recommendable! -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Fri, 12 Jan 2024 at 08:56, Left Right via Python-list wrote: > > By the way, in an attempt to golf this problem, I discovered this, > which seems like a parser problem: When you jump immediately to "this is a bug", all you do is make yourself look like an idiot. Unsurprisingly, this is NOT a bug, this is simply that you didn't understand what was going on. The grammar isn't easy to read, and it's usually better to read the documentation instead. (Plus, golfing isn't really a goal in Python, and you didn't shorten the code by much at all. Good job.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On 12/01/24 12:56, Chris Angelico via Python-list wrote: On Fri, 12 Jan 2024 at 08:56, Left Right via Python-list wrote: By the way, in an attempt to golf this problem, I discovered this, which seems like a parser problem: When you jump immediately to "this is a bug", all you do is make "seems"! but yes, it is a (much) less-likely explanation. yourself look like an idiot. Unsurprisingly, this is NOT a bug, this is simply that you didn't understand what was going on. The grammar isn't easy to read, and it's usually better to read the documentation instead. Those of us who studied Computer Science may well have been taught/expected to learn how to read [modified] BNF - indeed to have worked in that (cf coding in Python). Accordingly, the English text is likely easier to understand, but sometimes the BNF offers finer-detail or can be used to clarify some mis- or insufficiently-understood aspect of the text. IMHO/YMMV/etc... (Plus, golfing isn't really a goal in Python, and you didn't shorten the code by much at all. Good job.) I took my hat off to the poster, being prepared to dive-in and do this. Accordingly, was more than happy to help set him/her back onto 'the straight and narrow'. (yes it was a BNF-failing - which, credit where credit's due, I think was realised at the same time as response was typed) How many others just want us to do all their thinking for them? (there's a rude comment about wiping noses - but probably a step too far wrt the CoC) -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
Thursday, January 11, 2024 at 10:44, Rich Shepard via Python-list wrote: Re: Extract lines from file, add to (at least in part) >On Thu, 11 Jan 2024, MRAB via Python-list wrote: >> 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. If that is it all? a simple Grep would do (and save on the blank line) -- https://mail.python.org/mailman/listinfo/python-list