我是靠谱客的博主 感性豆芽,这篇文章主要介绍python mysql 数据记录转成json串,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*- import os import xlrd import datetime import json import sys import re import pymysql import pandas as pd parent_path = os.path.dirname(sys.path[0]) if parent_path not in sys.path: sys.path.append(parent_path) today = datetime.date.today().strftime("%Y-%m-%d") class Convert(): def __init__(self): pass def getdate(self, date): s_date = datetime.date(1899, 12, 31).toordinal() - 1 if isinstance(date, float): date = int(date) d = datetime.date.fromordinal(s_date + date) return d.strftime("%Y-%m-%d") def clean_new(self): self.web_tow = pymysql.connect(host='192.168.0.2', user='admin', passwd='admin', db='db', port=3306, charset='utf8', cursorclass=pymysql.cursors.DictCursor, connect_timeout=7200) # self.web_tow = pymysql.connect(host='127.0.0.1', user='root', passwd='root', # db='db', port=3306, charset='utf8', # cursorclass=pymysql.cursors.DictCursor, connect_timeout=7200) self.cursor_web_default_tow = self.web_tow.cursor() select_sql = """SELECT * FROM `table`""" data = [] # 写入数据库 self.cursor_web_default_tow.execute(select_sql) rows = self.cursor_web_default_tow.fetchall() for row in rows: id = row['id'] sheetName = row['sheet_name'] Codes = row['codes'] enable = row['enable'] benable = True if enable == 0: benable = False data.append( ( { "id": id, "name": sheetName, "list": list(str(Codes).split(',')), "enable": benable } ) ) print("""{"data":""" + str(json.dumps(data)) + "}") # 解决输出json过多,输出到txt with open("abc.txt", 'w') as f: f.write('') # json.dumps把对象中单引号转成双引号 f.write("""{"data":"""+str(json.dumps(data))+"}") if __name__ == '__main__': statis = Convert() statis.clean_new() statis.cursor_web_default_tow.close() statis.web_tow.close()

最后

以上就是感性豆芽最近收集整理的关于python mysql 数据记录转成json串的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部