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

前端 未结 4 927
孤城傲影
孤城傲影 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:51

    Use this library uszipcode.

    Advantages:

    • Data is up-to-date, super rich info, way richer and more up-to-date than zipcode and pyzipcode and any other python zipcode library.
    • Query is super easy, and there are like 20+ built-in query pattern you can use. And you can customize your query anyway you want.
    • Support fuzzy string match for city and state. You don't need to use the exact name.
    >>> from uszipcode import ZipcodeSearchEngine
    >>> search = ZipcodeSearchEngine()
    >>> zipcode = search.by_zipcode("10001")
    >>> print(zipcode)
    {
        "City": "New York", 
        "Density": 34035.48387096774, 
        "HouseOfUnits": 12476, 
        "LandArea": 0.62, 
        "Latitude": 40.75368539999999, 
        "Longitude": -73.9991637, 
        "NEBoundLatitude": 40.8282129, 
        "NEBoundLongitude": -73.9321059, 
        "Population": 21102, 
        "SWBoundLatitude": 40.743451, 
        "SWBoungLongitude": -74.00794499999998, 
        "State": "NY", 
        "TotalWages": 1031960117.0, 
        "WaterArea": 0.0, 
        "Wealthy": 48903.42702113544, 
        "Zipcode": "10001", 
        "ZipcodeType": "Standard"
    }
    
    # fuzzy city, state search, case insensitive, spelling mistake tolerant
    # all zipcode in new york
    >>> result = search.by_city_and_state(city="newyork", state="NY")
    >>> search.export_to_csv(result, "result.csv")
    

    Very easy to use to build advance search

    >>> result = search.find(city="new york", 
    ... wealthy=100000, sort_by="Wealthy", ascending=False, returns=10)
    

提交回复
热议问题