Parsing with Python

by Bill Maya on June 1, 2014

I’ve been given a list of contacts in vCard format of people interested in interactive storytelling. Here’s one of them.

BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.9.1//EN
N:Maya;Bill;;;
FN:Bill Maya
EMAIL;type=INTERNET;type=WORK;type=pref:bill.maya@gmail.com
CATEGORIES:Phrontisterion,Team
UID:bcbea367-d59d-493c-8f28-5f3a524a28b2
X-ABUID:E2B6D1B2-EF7C-4C24-926A-01973C141240:ABPerson
END:VCARD

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.

FN:Bill Maya
EMAIL;type=INTERNET;type=WORK;type=pref:bill.maya@gmail.com

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

Mission accomplished.

Bill Maya
bill.maya@gmail.com

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.

I definitely think Python is going to be my next new language.

{ 1 comment… read it below or add one }

Kef Schecter June 13, 2014 at 8:10 am

I’m a long-time Pythonista myself and I have a few comments:

You need parentheses when you call close. “fyle.close” does nothing; it simply evaluates to the method fyle.close. (This may seem nonsensical now, but should make more sense when you’re more experienced with the language.) Only after you add parentheses — fyle.close() — does it do anything.

But this is actually not the best way to handle files in Python. Better is to use the “with” statement, which — if this comment form won’t mangle the formatting — comes out to something like this:


with open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_1.txt') as fyle:
with open('C:\\Users\\billm_000\\Desktop\\Phrontisterion_Output.txt', 'w') as output:
for lyne in fyle:
nameId = "FN"
# etc. etc. etc.

When the code exits the ‘with’ blocks, it will call close() automatically. Not only does this mean that you won’t forget to close the file, it also means that close() will be called even if the code is interrupted in the middle and has to exit the function before the end of the end of the block (whether due to a return statement, an exception, or whatever). It’s a very clean way of doing things.

Leave a Comment

Previous post: