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 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 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 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
On Fri, 12 Jan 2024, AVI GROSS via Python-list wrote: But is the solution a good one for some purpose? The two output files may end up being out of sync for all kinds of reasons. One of many "errors" can happen if multiple lines in a row do not have an "@" or a person's name does, for example. What if someone supplied more than one email address with a comma separator? This may not be expected but could cause problems. Avi, For my use 1) the salutation and email address (always with an '@') are sequential and 2) I'm developing the script to extract both from the same file. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Fri, 12 Jan 2024, Rich Shepard via Python-list wrote: For my use 1) the salutation and email address (always with an '@') are sequential and 2) I'm developing the script to extract both from the same file. I've looked at my Python books "Python Crash Course," "Effective Python," and "Python Tricks The Book" as well as web pages in my searches without finding the answer to what may be a simple question: how to specify a variable in one file that has its values in another file. Specifically, how to I designate the salutation holder in the message file and pass it the name value from the name/email address file? If this explanation is not sufficiently clear I'll re-write it. :-) TIA, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Mon, 29 Jan 2024, dieter.mau...@online.de wrote: Have you read "https://docs.python.org/3/library/io.html#module-io";? Dieter, No, I hadn't ... but I am reading it now. Many thanks, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Mon, 29 Jan 2024, Rich Shepard via Python-list wrote: No, I hadn't ... but I am reading it now. Perhaps I missed the answer to my question when reading the io module. It explains how to open/write/read files of text and binary data, not passing a variable's value from one file to a place-keeper in another file. I'll keep searching for a solution. Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files [RESOLVED]
On Mon, 29 Jan 2024, Rich Shepard via Python-list wrote: I'll keep searching for a solution. IIRC, someone here pointed me to <https://realpython.com/python-send-email/> and I forgot about it ... until now. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Mon, 29 Jan 2024, avi.e.gr...@gmail.com wrote: There are several general solutions that may apply. Some involve reading in both files into data structures and perhaps linking them together in some way such as a data.frame or binary tree. You can then process individual request in memory/ Avi, I found several web pages describing how to use the python email library and tools to send individual or multiple email messages. I'll learn how to do this based on a couple of detailed examples. Thanks, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Mon, 29 Jan 2024, Thomas Passin via Python-list wrote: If you aren't going to use one or another existing template system, perhaps the easiest is to use unique strings in the message file. For example: Dear __##so-and-so##__: Please don't write this message off as mere spam. Respectfully, Rich Then you just do a replace of the unique string by the salutation. Don't change the original (i.e., template), make the changes to a copy that you will output. My script is not a web application, but an emailer that allows me to contact clients and prospective clients. From the command line on a linux host. Using the python smtplib and mail modules. Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote: Fine, my toy example will still be applicable. But, you know, you haven't told us enough to give you help. Do you want to replace text from values in a file? That's been covered. Do you want to send the messages using those libraries? You haven't said what you don't know how to do. Something else? What is it that you want to do that you don't know how? Thomas, For 30 years I've used a bash script using mailx to send messages to a list of recipients. They have no salutation to personalize each one. Since I want to add that personalized salutation I decided to write a python script to replace the bash script. I have collected 11 docs explaining the smtplib and email modules and providing example scripts to apply them to send multiple individual messages with salutations and attachments. Today I'm going to be reading these. They each recommend using .csv input files for names and addresses. My first search is learning whether I can write a single .csv file such as: "name1","address1" "mane2","address2" which I believe will work; and by inserting at the top of the message block Hi, {yourname} the name in the .csv file will replace the bracketed place holder. Still much to learn and the batch of downloaded PDF files should educate me. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
RE: Extract lines from file, add to new files
On Tue, 30 Jan 2024, AVI GROSS via Python-list wrote: But seriously, the OP, AKA Rich, is making clear that he is making a tool for his own use. It sounds like he wants to maintain a data repository of his own with some info about his clients and then have the ability to specify a name and pop up an email directed to them, or something along those lines. Close, Avi. I have no issues sending messages to single individuals or mailing lists. I want to send the same message to several individuals at one time, which I've done -- without individual salutations -- for 30 years using a bash script and mailx. As I replied to Thomas on the list, I've downloaded 11 PDF docs from the Web (and a useful book on the Python3 standard library) and will start reading and learning from them today. I expect to find answers to my few remaining questions in these docs. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Aw: Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Karsten Hilbert wrote: Why not foxus on just the part you think you are better off using python, namely personalization ? Create personalized files and send them with your trusted mailx solution ? Karsten, Too much time. And while mailx accepts the '-a' option for attachments but has none for individual salutations. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Aw: Re: Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Karsten Hilbert wrote: It doesn't need to. It just sends the (pre-personalized-by-Python) mail files. Karsten, In which case, I might as well have Python format and send the messages. :-) Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract lines from file, add to new files
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote: If I had a script that's been working for 30 years, I'd probably just use Python to do the personalizing and let the rest of the bash script do the rest, like it always has. The Python program would pipe or send the personalized messages to the rest of the bash program. Something in that ballpark, anyway. Thomas, A bash shell script looks easier for me and more promising. Using a while loop (one for the name file the other for the address file), and sed for putting the name at the head of the message replacing a generic placeholder should work with the existing for loop script. Thanks, Rich -- https://mail.python.org/mailman/listinfo/python-list