Address Parser

This one parses a list of addresses with just a bit of markup and generates kml for them.

import kml

def parse_file(data):
    #get rid of tabs
    data = data.replace('t', '')

    #get rid of spaces at the end of lines
    while data.find(' n') != -1:
        data = data.replace(' n', 'n')

    branches = data.split('*')
    
    parsed_branches = []
    for branch in branches:
        parsed_branch = parse_branch(branch)
        if not parsed_branch:
            continue #skip blank sections of the file
        parsed_branches.append(parsed_branch)
    return parsed_branches

def parse_branch(data):
    lines = data.split('n')

    location_lines = []
    contact_lines = []

    first_line = True
    for line in lines:
        if line.startswith('-'):
            line = line[1:] #remove the hypthen
            location_lines.append(line)
        if line.startswith('marker: '):
            #place the marker here, and don't show this line
            line = line[8:]
            location_lines = [line]
            continue
        if first_line:
            first_line = False
            title = line
            continue #don't show this line in the box
        if '@' in line:
            #assume entire line is an email address
            line = '<a href="mailto:%s">%s</a>' % (line, line)
        contact_lines.append(line)        

    if not title:
        return None #skip blank stuff

    contact = '<br />'.join(contact_lines)
    #remove extra line breaks in the contact info
    while contact.endswith('<br />'):
        contact = contact[:-6]

    location = ' '.join(location_lines)
    #if no address, use the title
    if not location:
        if title.endswith(' Branch'):
            location = title[:-7]
        else:
            location = title

    data = {
        'title': title,
        'contact': contact,
        'location': location,
        }
    return data

if __name__ == '__main__':
    f = file('branchdata.txt')
    data = f.read()
    f.close()

    branch_data = parse_file(data)

    k = kml.KML("Branches")
    for branch in branch_data:
        k.add_placemark(branch['title'], branch['contact'], branch['location'])

    f = file('branches.kml', 'w')
    f.write(str(k))
    f.close()
Advertisements