diff --git a/examples/README.md b/examples/README.md index dccdc39..bbf1bef 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1 +1,8 @@ # Python Examples + +Here we have put together a hand full of python examples to help get you started. As always feel free to [contact us](https://lob.com/support) directly if you have any questions on implementation. + + +## /csv_address_verification/ + +An example showing how to validate and cleanse a CSV spreadsheet full of shipping addresses using Lob's [Address Verifcation API](https://lob.com/verification/address). diff --git a/examples/csv_address_verification/input.csv b/examples/csv_address_verification/input.csv new file mode 100644 index 0000000..88fd289 --- /dev/null +++ b/examples/csv_address_verification/input.csv @@ -0,0 +1,9 @@ +address1,address2,city,state,postcode +185 Berry St,,San Francisco,CA,94107 +1135 burrisville rd,,centreville,MD,21617 +2101 Horizon Run,,The Villages,FL,32162 +7111 Woodmont Ave,Apt 908,Chevy Chase,MD,20815-6237 +8216 Monterra Ranch Cir,Apt 1504,Fort Worth,TX,76177-8540 +928b Rodney drive,,Nashville,TN,37205 +147 Bokum Rd,,Old Saybrook,CT,06475-1246 +111 NotAnAddress,,San Francisco,CA,94107 \ No newline at end of file diff --git a/examples/csv_address_verification/verify.py b/examples/csv_address_verification/verify.py new file mode 100644 index 0000000..1785dec --- /dev/null +++ b/examples/csv_address_verification/verify.py @@ -0,0 +1,65 @@ +# Usage +# python verify.py input.csv + +import lob +import csv +import sys + +lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc' + +skipFirstLine = True + +# Column indices in CSV +address1 = 0; +address2 = 1; +city = 2; +state = 3; +postcode = 4; +country = 'US' + +try: + sys.argv[1] +except IndexError: + print "Please provide an input CSV file as an argument." + sys.exit() + +# Open input files +inputFile = open(sys.argv[1], 'rU') +csvInput = csv.reader(inputFile) + +# Create output files +errors = open('errors.csv', 'w') +verified = open('verified.csv', 'w') + +# Loop through input CSV rows +for idx, row in enumerate(csvInput): + if skipFirstLine and idx == 0: + continue + + # Sanity check + sys.stdout.write('.') + sys.stdout.flush() + + try: + verifiedAddress = lob.Verification.create( + address_line1 = row[address1], + address_line2 = row[address2], + address_city = row[city], + address_state = row[state], + address_zip = row[postcode], + address_country = country + ) + except Exception, e: + outputRow = ",".join(row) + "," + str(e)+ "\n" + errors.write(outputRow) + else: + outputRow = verifiedAddress.address.address_line1 + "," + outputRow += verifiedAddress.address.address_line2 + "," + outputRow += verifiedAddress.address.address_city + "," + outputRow += verifiedAddress.address.address_state + "," + outputRow += verifiedAddress.address.address_zip + "\n" + verified.write(outputRow) + +errors.close() +verified.close() +print "\n"