Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting to CSV after python parse #105

Closed
BlvdJoe opened this issue Aug 6, 2015 · 2 comments
Closed

Exporting to CSV after python parse #105

BlvdJoe opened this issue Aug 6, 2015 · 2 comments

Comments

@BlvdJoe
Copy link

BlvdJoe commented Aug 6, 2015

First of all - Love this tremendously. Second, I've been trying to use the usaddress.py + csv.py + pandas to import from a csv, parse the rows, then export the rows back out into a csv.

However, when I run the code listed below, it (1) doesn't parse the column headers correctly and (2) doesn't then parse the fields correctly. This approached was recommended to me over on StackOverflow, so please let me know if (1) This approach is silly and/or (2) I'm merely suffering from noob-itis as I am, in fact, terribly new to python.

import pandas as pd
import usaddress
from pandas import DataFrame

data = ['Robie House, 5757 South Woodlawn Avenue, Chicago, IL 60637',
    'State & Lake, Chicago']

tagged_addresses = [usaddress.parse(line) for line in data]

address_df = pd.DataFrame(tagged_addresses)

address_df.to_csv('moooutput.csv')

image

@cathydeng
Copy link
Contributor

hey @BlvdJoe!

for what you're trying to do, the tag method would make more sense than the parse method. also, great timing with this question because I just wrote some code to do this task yesterday:

import csvkit
import usaddress

# expected format in input.csv: first column 'id', second column 'address'
with open('input.csv', 'rU') as f:
    reader = csvkit.DictReader(f)

    all_rows = []
    for row in reader:
        try:
            parsed_addr = usaddress.tag(row['address'])
            row_dict = parsed_addr[0]
        except:
            row_dict = {'error':'True'}

        row_dict['id'] = row['id']
        all_rows.append(row_dict)

field_list = ['id','AddressNumber', 'AddressNumberPrefix', 'AddressNumberSuffix', 'BuildingName', 
              'CornerOf','IntersectionSeparator','LandmarkName','NotAddress','OccupancyType',
              'OccupancyIdentifier','PlaceName','Recipient','StateName','StreetName',
              'StreetNamePreDirectional','StreetNamePreModifier','StreetNamePreType',
              'StreetNamePostDirectional','StreetNamePostModifier','StreetNamePostType',
              'SubaddressIdentifier','SubaddressType','USPSBoxGroupID','USPSBoxGroupType',
              'USPSBoxID','USPSBoxType','ZipCode', 'error']

with open('output.csv', 'wb') as outfile:
    writer = csvkit.DictWriter(outfile, field_list)
    writer.writeheader()
    writer.writerows(all_rows)

I just used csvkit to read from & write to csv. for the output, I also added an error column to record when addresses encounter a RepeatedLabelError.

@BlvdJoe
Copy link
Author

BlvdJoe commented Aug 7, 2015

@cathydeng Thank you!! I deleted my previous comment as I figured out what was causing my previous error. You're great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants