Python SQL查詢并生成json文件操作示例
本文實(shí)例講述了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:這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
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文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 基于python實(shí)現(xiàn)cdn日志文件導(dǎo)入mysql進(jìn)行分析
- Python之csv文件從MySQL數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出的方法
- 使用python將mdb數(shù)據(jù)庫(kù)文件導(dǎo)入postgresql數(shù)據(jù)庫(kù)示例
- Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫(kù)表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
- python 讀取excel文件生成sql文件實(shí)例詳解
- Python實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)中的SQL文件生成和導(dǎo)入
相關(guān)文章
Python統(tǒng)計(jì)時(shí)間內(nèi)的并發(fā)數(shù)代碼實(shí)例
這篇文章主要介紹了Python統(tǒng)計(jì)時(shí)間內(nèi)的并發(fā)數(shù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12PyQt5 designer 頁(yè)面點(diǎn)擊按鈕跳轉(zhuǎn)頁(yè)面實(shí)現(xiàn)
本文主要介紹了PyQt5 designer 頁(yè)面點(diǎn)擊按鈕跳轉(zhuǎn)頁(yè)面實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-019行Python3代碼實(shí)現(xiàn)批量提取PDF文件的指定內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何通過9行Python3代碼實(shí)現(xiàn)批量提取PDF文件的指定內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12python pandas 時(shí)間日期的處理實(shí)現(xiàn)
這篇文章主要介紹了python pandas 時(shí)間日期的處理實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python數(shù)據(jù)結(jié)構(gòu)輸入輸出及控制和異常
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)輸入輸出及控制和異常,上一章節(jié)中我們介紹了python的基礎(chǔ)數(shù)據(jù)類型和集合數(shù)據(jù)類型,這章節(jié)給大家介紹一下python的輸入輸出、控制和異常,對(duì)數(shù)據(jù)類型感興趣的同學(xué)可以查看一下文章<BR>2021-12-12Python統(tǒng)計(jì)日志中每個(gè)IP出現(xiàn)次數(shù)的方法
這篇文章主要介紹了Python統(tǒng)計(jì)日志中每個(gè)IP出現(xiàn)次數(shù)的方法,實(shí)例分析了Python基于正則表達(dá)式解析日志文件的相關(guān)技巧,需要的朋友可以參考下2015-07-07