My task is to take a full address and parse them out. I found a module called usaddress ( https://pypi.python.org/pypi/usaddress ) that will take '12345 Old Hillbilly LN Lot 7 Oklahoma City, OK 78945' and output the OrderedDict below.
How do I go about assigning and populating them to the correct field?
(OrderedDict([('AddressNumber', u'12345'), ('StreetName', u'Old Hillbilly'),
('StreetNamePostType', u'LN'), ('OccupancyType', u'Lot'), ('OccupancyIdentifier', u'7'),
('PlaceName', u'Oklahoma City'), ('StateName', u'OK'), ('ZipCode', u'78945"')]), 'Street Address')
This is what I have so far just to get it to populate one field called AddressOut but it errors.
import os
import usaddress
import arcpy
from collections import *
# Get feature class through parameter
fc = arcpy.env.workspace = arcpy.GetParameterAsText(0)
field1 = "FullAddress"
field2 = "AddressOut"
with arcpy.da.UpdateCursor(fc, field1,field2) as cursor:
for row in cursor:
row.setValue(field2, row.getValue(list(usaddress.tag(field1))) # field2 will parse field1
cursor.updateRow(row)

New code:
import usaddress
from collections import OrderedDict
# Get feature class through parameter
fc = arcpy.env.workspace = arcpy.GetParameterAsText(0)
field1 = "FullAddress"
field2 = "StreetName"
rows = arcpy.UpdateCursor(fc)
for row in rows:
addr_dict = usaddress.tag(row.getValue(field1))
x,y = addr_dict
#arcpy.AddMessage(addr_dict)
a = x[field2]
arcpy.AddMessage(a)
row.setValue(field2, a)
rows.updateRow(row)
del row
del rows
Answer
Ok, thanks for the extra info on the usaddress module. Most of your issues stem from the way you're using the cursor. Refer to this help and look closely at the examples.
Tips for you:
-- second argument for the cursor should be a list or tuple of field names, i.e. (field1,field2) not field1,field2 as you have.
-- the rows that the cursor returns are just lists of the attribute values for the fields that you specified when you instantiated the cursor. Which means that to access the value of field1, use this value = row[0] and to set the value use this row[0] = "new value" (.setValue() is not used in .da cursors).
-- regarding the dictionary itself, I'd recommend making that at the beginning of each row, and then accessing it by using the key you want:
with arcpy.da.UpdateCursor(fc,(field1,field2)) as cursor:
for row in cursor:
addr_dict = usaddress.tag(row[field1])
# say you want to put the address number in field 2
row[1] = addr_dict['AddressNumber']
cursor.updateRow(row)
That module looks really useful.
EDIT: another way to access the fields in the row:
fields = ("field_name_1","field_name_2")
with arcpy.da.UpdateCursor(fc,fields) as cursor:
for row in cursor:
field_1_value = row[fields.index("field_name_1")]
You are using the .index() method on the list of fields that you passed to the cursor in order to get an integer for the index position of that field in the list, then giving that index integer to the row to find the appropriate value.
No comments:
Post a Comment