用Python將mysql數(shù)據(jù)導(dǎo)出成json的方法
1、相關(guān)說(shuō)明
此腳本可以將Mysql的數(shù)據(jù)導(dǎo)出成Json格式,導(dǎo)出的內(nèi)容可以進(jìn)行select查詢(xún)確定。
數(shù)據(jù)傳入?yún)?shù)有:dbConfigName, selectSql, jsonPath, fileName。
依賴(lài)的庫(kù)有:MySQLdb、json,尤其MySQLdb需要事先安裝好。
2、Python腳本及測(cè)試示例
/Users/nisj/PycharmProjects/BiDataProc/oldPythonBak/mysqlData2json.py
# -*- coding=utf-8 -*- import MySQLdb import warnings import datetime import sys import json reload(sys) sys.setdefaultencoding('utf8') warnings.filterwarnings("ignore") mysqlDb_config = { 'host': 'MysqlHostIp', 'user': 'MysqlUser', 'passwd': 'MysqlPass', 'port': 50512, 'db': 'Tv_event' } today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) def getDB(dbConfigName): dbConfig = eval(dbConfigName) try: conn = MySQLdb.connect(host=dbConfig['host'], user=dbConfig['user'], passwd=dbConfig['passwd'], port=dbConfig['port']) conn.autocommit(True) curr = conn.cursor() curr.execute("SET NAMES utf8"); curr.execute("USE %s" % dbConfig['db']); return conn, curr except MySQLdb.Error, e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) return None, None def mysql2json(dbConfigName, selectSql, jsonPath, fileName): conn, curr = getDB(dbConfigName) curr.execute(selectSql) datas = curr.fetchall() fields = curr.description column_list = [] for field in fields: column_list.append(field[0]) with open('{jsonPath}{fileName}.json'.format(jsonPath=jsonPath, fileName=fileName), 'w+') as f: for row in datas: result = {} for fieldIndex in range(0, len(column_list)): result[column_list[fieldIndex]] = str(row[fieldIndex]) jsondata=json.dumps(result, ensure_ascii=False) f.write(jsondata + '\n') f.close() curr.close() conn.close() # Batch Test dbConfigName = 'mysqlDb_config' selectSql = "SELECT uid,name,phone_num,qq,area,created_time FROM match_apply where match_id = 83 order by created_time desc;" jsonPath = '/Users/nisj/Desktop/' fileName = 'mysql2json' mysql2json(dbConfigName, selectSql, jsonPath, fileName)
以上這篇用Python將mysql數(shù)據(jù)導(dǎo)出成json的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Mysql?json類(lèi)型字段Java+Mybatis數(shù)據(jù)字典功能的實(shí)踐方式
- Mysql如何對(duì)json數(shù)據(jù)進(jìn)行查詢(xún)及修改
- MySQL中JSON字段數(shù)據(jù)類(lèi)型詳解
- Mysql怎么存儲(chǔ)json格式數(shù)據(jù)詳解
- mysql根據(jù)json字段內(nèi)容作為查詢(xún)條件(包括json數(shù)組)檢索數(shù)據(jù)
- Mysql將查詢(xún)結(jié)果集轉(zhuǎn)換為JSON數(shù)據(jù)的實(shí)例代碼
- 使用python將mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法
- MySQL查詢(xún)和篩選存儲(chǔ)的JSON數(shù)據(jù)的操作方法
相關(guān)文章
Python修改Excel數(shù)據(jù)的實(shí)例代碼
Python修改Excel數(shù)據(jù)的方法。2013-11-11python機(jī)器學(xué)習(xí)Logistic回歸原理推導(dǎo)
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)Logistic回歸原理推導(dǎo),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python實(shí)現(xiàn)提取str字符串/json中多級(jí)目錄下的某個(gè)值
今天小編就為大家分享一篇python實(shí)現(xiàn)提取str字符串/json中多級(jí)目錄下的某個(gè)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02使用Python設(shè)置tmpfs來(lái)加速項(xiàng)目的教程
這篇文章主要介紹了使用Python設(shè)置tmpfs來(lái)加速項(xiàng)目的教程,文中給出方法使用Python腳本將tmpfs保存于內(nèi)存中的程序存儲(chǔ)到本地硬盤(pán)上,需要的朋友可以參考下2015-04-04Python提示[Errno 32]Broken pipe導(dǎo)致線程crash錯(cuò)誤解決方法
這篇文章主要介紹了Python提示[Errno 32]Broken pipe導(dǎo)致線程crash錯(cuò)誤解決方法,是ThreadingHTTPServer實(shí)現(xiàn)http服務(wù)中經(jīng)常會(huì)遇到的問(wèn)題,需要的朋友可以參考下2014-11-11利用Python統(tǒng)計(jì)每天敲了多少次鍵盤(pán)
每到年末各大App都會(huì)給你來(lái)一次年度總結(jié),最近突發(fā)奇想,鍵盤(pán)是每天必備的工具,為啥不給鍵盤(pán)也來(lái)個(gè)工作總結(jié),本文就來(lái)用Python統(tǒng)計(jì)一下每天敲了多少次鍵盤(pán)吧2024-04-04