1. 数据来源, 中华人民共和国民政部 官网有定期更新:
http://www.mca.gov.cn/article/sj/xzqh/2018/
2. 初步处理,拿到最新的行政区划代码,先改动下最后三行,便于将其作为城市一级的行政区来处理
710000 台湾省
810000 香港特别行政区
820000 澳门特别行政区
改为
710001 台湾
810001 香港
820001 澳门
3. 脚本生成需要的json文件就可以了,这个给个python的demo
import os
import codecs
with codecs.open('ChinaCities.txt', 'r', 'utf-8') as file_object:
lines = file_object.readlines()
allLine = '{\n"list":\n'+'[\n'
for index in range(len(lines)):
line = lines[index]
newLine = ''.join(line.split())
newLine = newLine.encode(encoding='utf-8')
newLine = '{"code" : "' \
+ newLine[0:6] \
+ '",' \
+ '"name" : "' + newLine[6:]
if index != len(lines) - 1:
newLine += '"},\n'
else:
newLine += '"}\n'
allLine += newLine
# print(newLine)
allLine += ']\n' + '}'
filename = './output.json'
with open(filename, 'w') as file_object:
file_object.write(allLine)
网友评论