这里很简单,我正在寻找一个轻量级的库,它允许我查找给定邮政编码的城市/州配对。我正在使用django FWIW。提前谢谢。
答案 0 :(得分:12)
试试pyzipcode。主页上的一个例子:
>>> from pyzipcode import ZipCodeDatabase
>>> zcdb = ZipCodeDatabase()
>>> zipcode = zcdb[54115]
>>> zipcode.zip
u'54115'
>>> zipcode.city
u'De Pere'
>>> zipcode.state
u'WI'
>>> zipcode.longitude
-88.078959999999995
>>> zipcode.latitude
44.42042
>>> zipcode.timezone
-6
答案 1 :(得分:7)
使用此库uszipcode。
优点:
zipcode
和pyzipcode
以及任何其他python邮政编码库更丰富,更新。>>> 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")
非常容易构建高级搜索
>>> result = search.find(city="new york",
... wealthy=100000, sort_by="Wealthy", ascending=False, returns=10)
答案 2 :(得分:3)
PYPI上最新版本的pyzipcode容易受到SQL注入攻击,因此使用this fork可能更好,这似乎解决了这些问题。
答案 3 :(得分:2)
我构建了Zipcodes来删除所有其他邮政编码库对SQLite的依赖。 SQLite在AWS Lambda环境中不可用,因此该库在包含有关美国邮政编码信息的gzip压缩JSON文件上提供了一个轻量级,功能强大的查询界面。以下是一些例子:
>>> # 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}]
>>> # Whether the zip-code exists within the database.
>>> print(zipcodes.is_valid('06463'))
False
>>> # 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...
]
>>> # 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'}]