I’ve been given a list of contacts in vCard format of people interested in interactive storytelling. Here’s one of them.
PRODID:-//Apple Inc.//Mac OS X 10.9.1//EN
I want to send out an email to everyone in this list to tell them the Phrontisterion Online blog has been rebooted. There’s somewhere between 30-40 email address so I probably could have cut and pasted each one into a text file but why go the brute force route when when it might possible to automate it. I’ve been thinking about learning Python for a side project so I thought I’d try to use it to parse the file’s contents.
Here’s the code that opens the file and prints out every line.
fyle = open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_1.txt') for lyne in fyle: print lyne fyle.close
But I’m only interested in the name and the email address. These lines are prefaced with “FN” and EMAIL” respectively. So lets just find those lines.
fyle = open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_1.txt') for lyne in fyle: nameId = "FN" if nameId in lyne: print lyne #2 emailId = "EMAIL" if emailId in lyne: print lyne #2 fyle.close
Here’s the result.
So now I just need to parse out the name and the email address from the individual lines and write those lines to a text file.
fyle = open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_1.txt') output = open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_Output.txt', 'w') #4 for lyne in fyle: nameId = "FN" if nameId in lyne: print lyne[3:] output.write(lyne[3:]) emailId = "EMAIL" if emailId in lyne: colonPosition = lyne.find(':') print lyne[colonPosition + 1:] output.write(lyne[colonPosition + 1:] + '\n') fyle.close output.close
It took me about 15-20 minutes to do this (less time than it took me to write this post). Of course Google was invaluable (what did developers ever do before the internet?). Here are some of the pages I used.
- Parsing a text file with Python
- Python: How to find a substring in another string
- String Manipulation in Python
- Python: Find position of a substring in a string
- Input and Output – Python
I definitely think Python is going to be my next new language.