get the Chinese city data
Project description
-
包介绍:Python语言的身份证前6位行政区划代码与地区名称对应。通过传入省市县(区)完整名称返回地区代码。
-
PIP安装:
pip install citydata
-
地区信息 json/dict 组成示例:
{ "id": "110101", "name": "东城区", "parentId": "110100", "shortName": "", "letter": "", "cityCode": "", "pinyin": "" }
-
API函数代码
# args 最多接受三个参数,顺序依次为省、市、县(区) def get_city_id(self, *args) -> tuple[str, ...]: if not args or len(args) > 3: raise ValueError("参数错误") # 一个参数,省级/市级/县(区)级 # 因存在重名县(区),不建议直接查询县(区)级 if len(args) == 1: province = args[0] for area in self.cityData: if area['name'] == province: province_id = area['id'] return province_id, raise ValueError("未能找到对应的行政区划代码,请检查参数") # 两个参数,省市两级或市县(区)级两级 elif len(args) == 2: province, city = args province_id = "0" for area in self.cityData: if area['name'] == province: province_id = area['id'] continue if area['name'] == city and area['parentId'] == province_id: city_id = area['id'] return province_id, city_id raise ValueError("未能找到对应的行政区划代码,请检查参数") # 三个参数,省市县(区)三级 else: province, city, county = args province_id, city_id = "0", "0" for area in self.cityData: if area['name'] == province: province_id = area['id'] continue if area['name'] == city and area['parentId'] == province_id: city_id = area['id'] continue if area['name'] == county and area['parentId'] == city_id: county_id = area['id'] return province_id, city_id, county_id raise ValueError("未能找到对应的行政区划代码,请检查参数")
-
引用举例
from citydata import CityData city_data = CityData() city_ids = city_data.get_city_id('河北省','石家庄市','裕华区') # 包含省\市\县(区)字样的完整行政区名,否则找不到结果 province_id = city_ids[0] # 130000 (str) city_id = city_ids[1] # 130100 (str) county_id = county_id[2] # 130108 (str)
-
TODO
- 城市简称
- 邮政编码
- 城市拼音
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
citydata-0.0.4.tar.gz
(40.6 kB
view details)
Built Distribution
citydata-0.0.4-py3-none-any.whl
(41.1 kB
view details)
File details
Details for the file citydata-0.0.4.tar.gz
.
File metadata
- Download URL: citydata-0.0.4.tar.gz
- Upload date:
- Size: 40.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e66ffbdc160e1021585af81c56c4740205772592c7fbb2d56f37b5d93f083323 |
|
MD5 | 978d7bd0409bdbce428f09b932e15b45 |
|
BLAKE2b-256 | 24a62aab4d2887698d7aaedd5863587a1518e8fd570d13d26162b866a634c973 |
File details
Details for the file citydata-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: citydata-0.0.4-py3-none-any.whl
- Upload date:
- Size: 41.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa5260681d3a14d3e48edd47a85217b4d03b0488bd87500c62268db1c57f38b5 |
|
MD5 | 7030f8ceecba241e7b0af0148ed6e959 |
|
BLAKE2b-256 | 4c1936f8e184ce8d8f5a22465d852050e5323edfacb37472a8e8f58d3fa17e23 |