# Python address data parsing example # Uses the following metadata to parse the address data in AddressData.txt # Metadata: # Field firstName lastName StNumber StName City State Zip Zip+4 # Width 10 20 5 25 20 2 5 4 myFile = open('AddressData.txt', 'r') # open text file for reading for line in myFile: # for each line in the file, firstName = line[0:10] # use the metadata to parse into fields lastName = line[20:30] StreetNumber = line[30:35] streetName = line[35:60] city = line[60:80] state = line[80:82] zipCode = line[82:87] zip4 = line[87:91] # Print the data in each field. All strings have the method lstrip() and # rstrip() which trim leading and trailing spaces in a string. Both lstrip() # and rstrip() are non-destructive; they don't replace the data in the # string variable they act upon. print "First name: " + firstName.lstrip() print "Last name: " + lastName.lstrip() print "Steet number: " + StreetNumber.lstrip() print "Street name: " + streetName.lstrip() print "city: " + city.lstrip() print "state: " + state.lstrip() print "ZIP: " + zipCode.lstrip() print "ZIP+4: " + zip4.lstrip()