21 November Python Code to Enhance Zip Codes November 21, 2022 By Ricardo Rangel Tutorials Python, Zip Code API This example walks us trough a very simple Python Code that opens a file with a list of zip codes and creates a new CSV file that includes additional details related to the zip code. You can find this example in the Zip Code github repository. Let's dive right into it. Pattern: The first thing we'll do is make sure we have an API Key for using the Zip Code API. You can get a free trial here. Code Review import requests import csv We need to use the Python libraries "requests" (to manage API calls) and "csv" to easily manipulate csv files. vheaders = {"Ocp-Apim-Subscription-Key": ""} In this section you need to replace the value between the quotes to your API Key. This information is passed in the header section of the REST API call. with open(r'sample-zips.txt', 'r') as fp: with open('zip-enhanced.csv', 'w',newline='') as f: writer = csv.writer(f) Using the "with" command we'll open the sample-zips.txt file for "read" and we'll open the file zip-enhanced.csv file for write (note: if the file zip-enhanced.csv exists already, it will be replaced when the code executes). for line in fp: vzipcode = line.strip() url = f"https://global.metadapi.com/zipc/v1/zipcodes/{vzipcode}" response = requests.get(url,headers=vheaders).json() csvline = [vzipcode,response["data"]["stateCode"],response["data"]["stateName"], response["data"]["titleCaseCountyName"],response["data"]["latitude"],response["data"]["longitude"] ] writer.writerow(csvline) In this section, we have a loop for each line of the sample-zips.txt, the first step is to assign to the variable vzipcode the current line (minus any special characters for end of line). We then build the endpoint to get details of the zip code (the string value of the endpoint in the variable url). Next we call the API and assign it to object "response", it's a GET request to the endpoint defined in "url" and we pass the zip code api key in the header. We now create a csv line (i.e. comma separated attributes) and we look for specific attributes of the zip code that we need (the state code, the state name, the county of the zip code, the latitude and longitude). You can see the full schema of the Zip Code response here, there are many other attributes to serve a multitude of needs. Get the Zip Code API and start building an awesome solution! Try it Now! Related Posts Getting Started Zip Code API This tutorial shows how to get started with the Zip Code API. Dynamically Invoking REST API with Data Factory This tutorial walks trough the process of setting up a Data Factory Pipeline and invoking a REST API (using the Zip Code API as an example) as a lookup to enhance the data within the pipeline. What is a Zip Code? This article explains the basic information about zip codes in the United States of America with a history of how they were created, and how they are used today. Postman Tutorial If you are working with API’s, regardless of they type of project (data pipelines, application development, report development), you may need to work with API’s to uncover all the capabilities the API has to offer. Postman will allow you to quickly and easily get results from an API. We’ll also briefly describe other key capabilities of the tool that can be used to test code, design API’s, share additional information etc. At the end we also provide additional training resources for Postman. County Lookup by Zip Code This article aims to delve into the numerous business benefits associated with utilizing our county lookup by zip code feature in the Zip Code API. We will explore how this API can empower businesses to enhance their customer targeting, expand market reach, optimize logistics, and stay ahead of the competition. Please enable JavaScript to view the comments powered by Disqus. blog comments powered by Disqus