In this post I am going to show you how to take a text file full of raw mailing address data, parse each one into its component parts, and then run them through our address validation service. At the end of the tutorial you will have an output.csv file that contains valid mailing addreses split into address, city, state, zip, and country fields.
Just want the code? Check out our GitHub Repository
gem install StreetAddress
gem install lob
This is the input file we will be feeding to our address parsing script. The script assumes it will be named input.txt.
As a result, you should make sure that this file is a plain text file with one address per a line.
Create a blank file called verify.rb and lets get started.
# Initialize Lob object
Lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc'
@lob = Lob.load
First thing we need to do is include the necessary gems and then initialize the Lob ruby library. You can find your API key here.
# Open our output.csv for writing
output = File.open(File.expand_path('../output.csv', __FILE__), 'w')
# Add the CSV column headers
output.puts ['address_line1', 'address_city', 'address_state', 'address_zip', 'address_country'].join(',')
Next we want to create the output.csv file that will contain our parsed and validated addresses.
# Itterate through our input file.
File.open(File.expand_path('../input.txt', __FILE__)).each_line do |line|
parsed_address = StreetAddress::US.parse(line)
verified_address = @lob.addresses.verify(
Finally, we open up the input.txt file and itterate through each line in the file.
For each address line, we first parse the address using the StreetAddress gem. We then take the component parts of the address and pass it to the Lob Address Verification Service.
Once the address has been parsed and verified we then write it to our output.csv file.
Open up your terminal and run:
If all goes as expected, you should now see an output.csv file in our directory. If you are running into any issues, leave a comment and we will get back to you.