Country State City List Csv Download Option

If you're only interested in the files and without implementation details, then you can grab them.

The state or territory's USPS postal abbreviation. Statename: The name of the state or territory that contains the city/town. Countyfips: The 5-digit FIPS code for the primary county. The first two digits correspond to the state's FIPS code. Countyname: The name of the primary county (or equivalent) that contains the city/town. I can never find a list of states (in the United States) for an HTML select/option list (aka, combobox, drop-down list) when I need one, so I’m putting two versions here. HTML select/option, United States list: abbreviations only. The first select/option list shows a list of states, but only shows the abbreviations of the states.

Every now and then, I end up working on a project that requires a form that includes county, city, and state information. More often than not, the client doesn’t about the rich data associated with any of the above – that is, they’re not particularly interested in latitude, longitude, population, or even zip code.

Us state list csv

Simply put, they want the user to be able to select their country, select their city, and then select their state from a predefined list of options.

For the past couple of projects, I’ve been using three, very simple CSV’s for this and thought I’d share them here should anyone else want to use them. Additionally, I thought I’d share a simple implementation of how they can be used in WordPress projects.

Country State and City CSV

Here are the “County State City CSV” files. The archive includes:

  • countries.csv which lists country names and the country abbreviations.
  • states.csv includes all US states and their abbreviations
  • cities.csv has a list of all current US cities.

Note that with the exception of the country csv file, the cities and the states and United States only. This is because the majority of the work I do is for projects, ahem, here in the United States.

Implementation in WordPress

Since this data is kept in external files, and since manually managing select elements (or autocomplete elements, etc.) could get cumbersome with exceptionally long lists – like the city list – I usually load this data via Ajax.

If you’re a WordPress developer, here’s an example of how you can populate a select box with, say, the countries.csv file.

Mark It Up

First, go ahead and stub out the select element that will be used for the countries:

Request It

Next, write a small JavaScript function that does the following:

  • Make an Ajax request to the URL where the countries.csv file is kept
  • Iterates through the list of codes and parses it
  • Creates option elements
  • Appends them to the select element created above

The comments should clarify what’s going on the in code below:

Once done, the select element will be populated with this of countries from the CSV.

Performance, JSON, and All That Jazz

Because I know there are n-number of ways to improve what’s above, I thought I’d tackle a couple of those points here.

State

Page Load Time

Performing this process on a page load every time the page is loading can be taxing, so you could work up an alternative implementation such as caching the values in some way (like storing the countries as a transient), a set of options, or even defining a lookup table (if the list is that large).

Why Not JSON?

I love JSON. In fact, when it comes to sending data to and from the client and the server, it’s my format of choice. The thing is, it’s far more common to find country, city, and state data in CSV formats – ranging from simple to very, very detailed – than it is to find it in JSON format.

Sure, you could convert the list to JSON and be just as well.

All That Jazz

Regardless of how you implement this, these three files have been useful for me in recent projects, so I thought I’d make them available here alone with a single example of how it could be used.

Sure, there are more – some more optimized than others – but either way, grab the files and implement them however you see fit.

Any help is welcome!

Project maintained by mledozeHosted on GitHub Pages — Theme by mattgraham

Countries data

This repository contains lists of world countries in JSON, CSV and XML. Each line contains the country:

  • name - common - common name in english - official - official name in english - native - list of all native names - key: three-letter ISO 639-3 language code - value: name object + key: official - official name translation + key: common - common name translation
  • country code top-level domain (tld)
  • code ISO 3166-1 alpha-2 (cca2)
  • code ISO 3166-1 numeric (ccn3)
  • code ISO 3166-1 alpha-3 (cca3)
  • code International Olympic Committee (cioc)
  • ISO 4217 currency code(s) (currency)
  • calling code(s) (callingCode)
  • capital city (capital)
  • alternative spellings (altSpellings)
  • region
  • subregion
  • list of official languages (languages) - key: three-letter ISO 639-3 language code - value: name of the language in english
  • list of name translations (translations) - key: three-letter ISO 639-3 language code - value: name object + key: official - official name translation + key: common - common name translation
  • latitude and longitude (latlng)
  • name of residents (demonym)
  • landlocked status (landlocked)
  • land borders (borders)
  • land area in km² (area)

Additional data

The data folder contains additional data such as the countriesGeoJSON outlines and flags in SVG format.

Examples

JSON
GeoJSON outline

See an example for Germany. Ea serial key for alice madness returns.

CSV
XML
YAML

Customising the output

The data files provided in the dist directory include all available fields, but is also possible to build a custom version of the data with certain fields excluded.

To do this, you will first need a working PHP installation, composer and a local copy of this repository. Once you have these, open a terminal in your local version of this project’s root directory and run this command to install the necessary dependencies:

After this finishes, run the following command (here we will exclude the tld field from the output, but you can exclude any field you want):

You can also exclude multiple fields:

Showcase

Projects using this dataset:

How to contribute?

Please refer to CONTRIBUTING.

To do

  • add the type of the country (country, sovereign state, public body, territory, etc.)
  • add missing translations

Sources

http://www.currency-iso.org/ for currency codes.

Country Code List Csv

Region and subregion are taken from https://github.com/hexorx/countries.

GeoJSON outlines come from http://thematicmapping.org/downloads/world_borders.php.

The rest comes from Wikipedia.

Credits

Thanks to:

Country State City List Csv Download Options

  • @Glazz for his help with country calling codes
  • @hexorx for his work (https://github.com/hexorx/countries)
  • @frederik-jacques for the capital cities
  • @fayer for the population, geolocation, demonym and area data
  • @ancosen for his help with the borders data
  • @herrniemand for country names and various fixes
  • all the contributors: https://github.com/mledoze/countries/graphs/contributors

License

Country State City List Csv Download Optional

See LICENSE.