Skip to content

Commit

Permalink
update decode utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
lyhmyd1211 authored Jun 3, 2020
1 parent 8afd3de commit 30e0940
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions getMap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: UTF-8 -*-

import json
import urllib.request
import urllib.parse
Expand All @@ -16,17 +18,21 @@ def getAreaCodeType(code):

if not os.path.isdir("province"):
os.mkdir("province")
print("创建province文件夹")
if not os.path.isdir("citys"):
os.mkdir("citys")
print("创建citys文件夹")
if not os.path.isdir("county"):
os.mkdir("county")
print("创建county文件夹")
context = ssl._create_unverified_context()
url = "https://geo.datav.aliyun.com/areas_v2/bound/infos.json"
with urllib.request.urlopen(url, context=context) as response:
html = json.loads(response.read().decode("utf-8"))
with open("location.json", "w") as json_file:
json.dump(html, json_file)
print("写入 location.json")
print("请求接口并写入文件中。。。")
html = json.loads(response.read().decode("UTF-8"))
with open("location.json", "w", encoding='utf-8') as json_file:
json.dump(html, json_file, ensure_ascii=False)
print("写入 location.json", html)
for item in list(html.keys()):
if getAreaCodeType(item) == "3":
with urllib.request.urlopen(
Expand All @@ -36,8 +42,8 @@ def getAreaCodeType(code):
print(
"请求:https://geo.datav.aliyun.com/areas_v2/bound/" + item + ".json"
)
text = json.loads(res.read().decode("utf-8"))
with open("county/" + item + ".json", "w") as json_file:
text = json.loads(res.read().decode("UTF-8"))
with open("county/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file)
print("写入", item)
else:
Expand All @@ -50,16 +56,17 @@ def getAreaCodeType(code):
+ item
+ "_full.json"
)
text = json.loads(res.read().decode("utf-8"))
text = json.loads(res.read().decode("UTF-8"))
if item == "100000":
with open("china.json", "w") as json_file:
json.dump(text, json_file)
with open("china.json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
elif getAreaCodeType(item) == "1":
with open("province/" + item + ".json", "w") as json_file:
json.dump(text, json_file)
with open("province/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
elif getAreaCodeType(item) == "2":
with open("citys/" + item + ".json", "w") as json_file:
json.dump(text, json_file)
with open("citys/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
print("写入", item)
print("写入完成")

0 comments on commit 30e0940

Please sign in to comment.