lightweight python library to query city/state name by zip code?

前端 未结 4 915
孤城傲影
孤城傲影 2021-02-14 08:20

Pretty simple here, I\'m looking for a lightweight library that will allow me to lookup a city/state pairing for a given zip code. I am using django FWIW. Thanks in advance.

4条回答
  •  日久生厌
    2021-02-14 08:55

    I built Zipcodes to remove the dependency on SQLite that all other zipcode libraries had. SQLite is not available in an AWS Lambda environment, so this library provides a lightweight, powerful querying interface over a gzipped JSON file containing U.S. zipcode data. Below are some examples:

    Matching:

    >>> # Handles of Zip+4 zip-codes nicely. :)
    >>> pprint(zipcodes.matching('77429-1145'))
    [{'zip_code': '77429',
      'zip_code_type': 'STANDARD',
      'city': 'CYPRESS',
      'state': 'TX',
      'lat': 29.96,
      'long': -95.69,
      'world_region': 'NA',
      'country': 'US',
      'active': True}]
    

    Validity:

    >>> # Whether the zip-code exists within the database.
    >>> print(zipcodes.is_valid('06463'))
    False
    

    Similarity:

    >>> # Search for zipcodes that begin with a pattern.
    >>> pprint(zipcodes.similar_to('0643'))
    [{'active': True,
      'city': 'GUILFORD',
      'country': 'US',
      'lat': 41.28,
      'long': -72.67,
      'state': 'CT',
      'world_region': 'NA',
      'zip_code': '06437',
      'zip_code_type': 'STANDARD'},
     {'active': True,
      'city': 'HADDAM',
      'country': 'US',
      'lat': 41.45,
      'long': -72.5,
      'state': 'CT',
      'world_region': 'NA',
      'zip_code': '06438',
      'zip_code_type': 'STANDARD'},
    ... # remaining results truncated for readability...
    ]
    

    Advanced filtering:

    >>> # Arbitrary nesting of similar_to and filter_by calls, allowing for great precision while filtering.
    >>> pprint(zipcodes.similar_to('2', zips=zipcodes.filter_by(zipcodes.list_all(), active=True, city='WINDSOR')))
    [{'active': True,
      'city': 'WINDSOR',
      'country': 'US',
      'lat': 33.48,
      'long': -81.51,
      'state': 'SC',
      'world_region': 'NA',
      'zip_code': '29856',
      'zip_code_type': 'STANDARD'},
     {'active': True,
      'city': 'WINDSOR',
      'country': 'US',
      'lat': 36.8,
      'long': -76.73,
      'state': 'VA',
      'world_region': 'NA',
      'zip_code': '23487',
      'zip_code_type': 'STANDARD'},
     {'active': True,
      'city': 'WINDSOR',
      'country': 'US',
      'lat': 36.0,
      'long': -76.94,
      'state': 'NC',
      'world_region': 'NA',
      'zip_code': '27983',
      'zip_code_type': 'STANDARD'}]
    

提交回复
热议问题