Python⽣成json⽂件1.数据准备
2.python代码
import  datetime
import  os
import  mssqlhelper
ms = mssqlhelper.MSSQL(host="192.168.0.108", user="sa", pwd="sa", db="ComPrject")
def getAreas(cityid):
arealist=ms.ExecQuery("select *From  dbo.areas  where  cityid='%s' " % cityid)
return arealist
def getCity(provinces):
citylist=ms.ExecQuery("select *From  dbo.cities where provinceid='%s'" % provinces)
return citylist
def getProvinces():
provlist=ms.ExecQuery("select *From  dbo.provinces")
return provlist
def createFileJson():
date=w().strftime('%Y-%m-%d')
path=date+'-provinces.json'
return path
def writeJson(path):
provlist=getProvinces()
with open(path,"w+",encoding="utf-8") as  f:
f.write("[")
lp = 0
for p in provlist:
if lp>0:
f.write(",\n")
else:
f.write("\n")
f.write("{\n")
f.write('"Code":"%s"\n'% p[1])
f.write(',"Name":"%s"\n'% p[2])
f.write(',Nodes:[\n')
citylist=getCity(p[1])
lc = 0
for c in citylist:
if lc>0:
f.write("\t,\n")
else:
f.write("\n")
f.write("\t{\n")
f.write('\t"Code":"%s"\n'% c[1])
f.write('\t,"Name":"%s"\n'% c[2])
f.write('\t,Nodes:[\n')
arealist = getAreas(c[1])
la = 0
for a in arealist:
if la>0:
f.write("\t\t,\n")
else:
f.write("\n")
f.write("\t\t{\n")
python怎么读取json文件f.write('\t\t"Code":"%s"\n'% a[1])
f.write('\t\t,"Name":"%s"\n'% a[2])
f.write("\t\t}\n")
la += 1
f.write("\t]\n")
f.write("\t}\n")
lc += 1
f.write("]\n")
f.write("}\n")
lp += 1
f.write("]\n")
if__name__ == '__main__':
path=createFileJson()    writeJson(path)
3.⽣成预览

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。