The ZIP Code Validator is a Python script that validates ZIP codes using the USPS City/State Lookup API. It takes an input CSV file containing ZIP codes, retrieves the corresponding city, state, and county information, and writes the results to an output CSV file.
- Validates ZIP codes using the USPS API.
- Provides city, state, and placeholder county information.
- Handles errors gracefully, marking invalid or failed lookups.
- Processes input CSV files and outputs results in a structured format.
- Python 3.7 or higher
- Modules:
requests
,csv
,xml.etree.ElementTree
- USPS User ID (register for free at USPS Registration)
- Clone or download the repository.
- Install the required Python packages:
pip install requests
-
Register for a USPS User ID if you don’t have one. Visit USPS Registration to sign up.
-
Replace the placeholder
USPS_USER_ID
in the script with your USPS User ID. -
Prepare an input CSV file named
zipcodes.csv
with ZIP codes in the first column. Example:ZIP Code 10001 90210
-
Run the script:
python zip_code_validator.py
-
The output will be saved in a file named
validated_zipcodes.csv
with the following format:ZIP Code,City,State,County 10001,New York,NY,Unavailable 90210,Beverly Hills,CA,Unavailable
- Update the variable
USPS_USER_ID
in the script with your USPS User ID.
- The script marks the county as "Unavailable" because the USPS API does not provide this information. You may extend the script to fetch county data from other APIs if needed.
This project is open-source and licensed under the MIT License.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
This script is intended for educational purposes. Ensure compliance with USPS API usage terms and conditions.