最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python SQL查詢并生成json文件操作示例

 更新時間:2018年08月17日 11:54:53   作者:sulin  
這篇文章主要介紹了Python SQL查詢并生成json文件操作,涉及Python基于mssqlhelper模塊進(jìn)行mysql數(shù)據(jù)庫查詢與json格式文件生成相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Python SQL查詢并生成json文件操作。分享給大家供大家參考,具體如下:

1. 數(shù)據(jù)準(zhǔn)備

SQL數(shù)據(jù)點(diǎn)擊此處本站下載

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=datetime.datetime.now().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")
          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.生成預(yù)覽

PS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作json技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

古浪县| 平山县| 巴中市| 贵港市| 兖州市| 南投市| 曲阳县| 绥棱县| 小金县| 高唐县| 习水县| 福海县| 新河县| 衡阳市| 武隆县| 双柏县| 大名县| 庆安县| 宜君县| 平乡县| 南溪县| 江达县| 聊城市| 青龙| 梅河口市| 桐柏县| 桂东县| 澄城县| 西充县| 南川市| 上犹县| 汤原县| 安徽省| 沧源| 商南县| 乌拉特后旗| 轮台县| 米泉市| 朝阳市| 扎鲁特旗| 图木舒克市|