问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

python怎么转化成json格式

发布网友 发布时间:2022-04-19 15:06

我来回答

2个回答

懂视网 时间:2022-05-02 02:18

CREATE TABLE 2 prpcitem_car 3 ( 4 proposalno CHAR(22) NOT NULL, 5 itemno DECIMAL(8,0) NOT NULL, 6 riskcode CHAR(3) NOT NULL, 7 insuredtypecode CHAR(2), 8 carinsuredrelation CHAR(1), 9 clausetype CHAR(3), 10 licenseno VARCHAR(20), 11 licensetype CHAR(3), 12 licensecolorcode CHAR(2), 13 nonlocalflag CHAR(1), 14 licenseflag CHAR(1), 15 vehicletype CHAR(4), 16 carkindcode CHAR(3), 17 hkflag CHAR(1), 18 hklicenseno CHAR(20), 19 engineno VARCHAR(30), 20 vinno VARCHAR(18), 21 frameno VARCHAR(30), 22 runareacode CHAR(10), 23 runareaname VARCHAR(60), 24 runmiles DECIMAL(14,2), 25 enrolldate DATE, 26 useyears DECIMAL(15,255), 27 vehiclecategory CHAR(3), 28 columvehiclecategoryn_68 CHAR(3), 29 modelcodealias VARCHAR(100), 30 modelcode CHAR(14), 31 brandname VARCHAR(200) NOT NULL, 32 aliasname VARCHAR(100), 33 countrynature CHAR(2), 34 countrycode CHAR(3), 35 usenaturecode CHAR(3), 36 businessclasscode CHAR(1), 37 seatcount DECIMAL(15,255), 38 toncount DECIMAL(10,3), 39 exhaustscale DECIMAL(8,4), 40 cylindercount INTEGER, 41 iscriterion INTEGER, 42 lossratio CHAR(5), 43 salesname VARCHAR(50), 44 salesnumber VARCHAR(20), 45 salesphone VARCHAR(15), 46 carcounts INTEGER, 47 isdropinvisitinsure INTEGER, 48 carloteququality DECIMAL(14,2), 49 colorcode CHAR(6), 50 safedevice CHAR(30), 51 coefficient1 DECIMAL(8,4), 52 coefficient2 DECIMAL(8,4), 53 coefficient3 DECIMAL(8,4), 54 othernature VARCHAR(10), 55 ratecode CHAR(8), 56 rationname VARCHAR(60), 57 makedate DATE, 58 carusage VARCHAR(20), 59 currency CHAR(3), 60 purchaseprice DECIMAL(14,2), 61 actualvalue DECIMAL(14,2), 62 invoiceno CHAR(20), 63 carloanflag CHAR(1), 64 cardealercode CHAR(16), 65 cardealername VARCHAR(120), 66 remark VARCHAR(40), 67 carid CHAR(10), 68 versionno CHAR(4), 69 monopolyflag CHAR(1), 70 monopolycode VARCHAR(22), 71 monopolyname VARCHAR(80), 72 newcarflag CHAR(1), 73 loanvehicleflag CHAR(1), 74 transfervehicleflag CHAR(1), 75 transferdate DATE, 76 operationarea VARCHAR(5), 77 inserttimeforhis DATETIME YEAR TO SECOND, 78 operatetimeforhis DATETIME YEAR TO SECOND, 79 startsitename VARCHAR(255), 80 discounttype VARCHAR(255), 81 drivertype VARCHAR(255), 82 endsitename VARCHAR(255), 83 carcusttype CHAR(2), 84 nodamageyears VARCHAR(4), 85 modeldemandno VARCHAR(50), 86 energytype CHAR(1), 87 fueltype CHAR(2), 88 carprooftype CHAR(2), 89 carproofno CHAR(50), 90 carproofdate DATE, 91 certificatedate DATE, 92 isremote CHAR(1), 93 fullendor CHAR(1), 94 issilageharvester CHAR(1), 95 flag CHAR(10), 96 licenseno1 CHAR(10), 97 licenseno2 VARCHAR(20), 98 licenseno3 CHAR(10), 99 vehiclebrand VARCHAR(20), 100 vehicletypedescription VARCHAR(5), 101 groupcode VARCHAR(50), 102 groupstartdate DATE, 103 groupenddate DATE, 104 PRIMARY KEY (proposalno, itemno) CONSTRAINT pk_citem_car, 105 FOREIGN KEY (proposalno) REFERENCES prpcmain (proposalno) CONSTRAINT fkb28a3afd9b27a158 106 ); View Code

对应python语句:

技术分享
 1 import json
 2 def get_list(sqlLines):
 3 list =[]
 4 str =‘‘
 5 table_name=‘‘
 6 primary_key=‘‘
 7 foreign_key=‘‘
 8 for line in sqlLines:
 9  if ‘PRIMARY KEY‘ in line:
10  primary_key=line[line.find(‘(‘)+1:line.find(‘)‘)]
11  if ‘FOREIGN KEY‘ in line:
12  foreign_key=line[line.find(‘(‘)+1:line.find(‘)‘)]
13  str+=line
14  list.append(str)
15  list.append(primary_key)
16  list.append(foreign_key)
17 return list
18 
19 def get_json(list):
20 str=list[0]
21 primary_key=list[1]
22 foreign_key=list[2]
23 table_name=str[str.find(‘CREATE TABLE‘)+len(‘CREATE TABLE‘):str.find(‘(‘)].strip()
24 str= str[str.find(‘(‘)+1:str.rfind(‘)‘)].strip()
25 str =str.lower()
26 str =str.replace(‘ not null‘,‘‘).strip()
27 if ‘primary key‘ in str:
28  str=str[0:str.find(‘primary key‘)]
29 if ‘foreign key‘ in str:
30  str=str[0:str.find(‘foreign key‘)]
31 if ‘datetime year to second‘ in str:
32  str= str.replace(‘datetime year to second‘,‘string‘)
33 
34 str_lines =str.splitlines(False)
35 json_list=[]
36 for line in str_lines:
37  json_dict={}
38  lines =line.split()
39  if len(lines)>=2:
40  name=lines[0]
41  type=lines[1][0:len(lines[1])-1]
42  json_dict[‘name‘]=name
43 
44  if ‘date‘ in type:
45   type=‘string‘
46  if ‘decimal‘ in type:
47   type=‘string‘
48  json_dict[‘type‘]=type
49  if name in primary_key:
50   json_dict[‘primary‘]=1
51  if name in foreign_key:
52   json_dict[‘foreign‘]=1
53  else:
54  continue
55  json_list.append(json_dict)
56 return json_list
57 
58 
59 
60 def load():
61 sqlStr =open(‘C:/Users/Administrator.PC--20150529IGF/Desktop/prpcitem_car.txt‘)
62 sqlLines =sqlStr.readlines(100000)
63 sql_list =get_list(sqlLines)
64 str =get_json(sql_list)
65 jsonS =json.dumps(str)
66  print(jsonS)
67 load()
View Code

---------------------

第二种sql格式:

技术分享
-- Start of generated script for 10.136.1.5-DB2-LIS (db2inst1)
-- Apr-10-2017 at 16:35:18

CREATE TABLE "DB2INST1"."LLCASE"
 ("CASENO"  VARCHAR(20) NOT NULL,
 "RGTNO"  VARCHAR(20),
 "RGTTYPE"  VARCHAR(1) NOT NULL,
 "RGTSTATE" VARCHAR(2) NOT NULL,
 "CUSTOMERNO" VARCHAR(24) NOT NULL,
 "CUSTOMERNAME" VARCHAR(120),
 "ACCIDENTTYPE" VARCHAR(1),
 "RECEIPTFLAG" VARCHAR(1),
 "HOSPITALFLAG" VARCHAR(1),
 "SURVEYFLAG" VARCHAR(1),
 "RGTDATE"  DATE,
 "HANDLEDATE" DATE,
 "CLAIMCALDATE" DATE,
 "AFFIXGETDATE" DATE,
 "FEEINPUTFLAG" VARCHAR(1),
 "INHOSPITALDATE" DATE,
 "OUTHOSPITALDATE" DATE,
 "INVALIHOSDAYS" SMALLINT,
 "INHOSPITALDAYS" SMALLINT,
 "DIANOSEDATE" DATE,
 "POSTALADDRESS" VARCHAR(300),
 "PHONE"  VARCHAR(20),
 "ACCSTARTDATE" DATE,
 "ACCIDENTDATE" DATE,
 "ACCIDENTSITE" VARCHAR(60),
 "DEATHDATE" DATE,
 "CUSTSTATE" VARCHAR(2),
 "ACCDENTDESC" VARCHAR(6000),
 "CUSTBIRTHDAY" DATE,
 "CUSTOMERSEX" VARCHAR(1),
 "CUSTOMERAGE" DECIMAL(5, 0),
 "IDTYPE"  VARCHAR(1),
 "IDNO"  VARCHAR(20),
 "HANDLER"  VARCHAR(10),
 "UWSTATE"  VARCHAR(10),
 "DEALER"  VARCHAR(10),
 "APPEALFLAG" VARCHAR(1),
 "TOGETHERGET" VARCHAR(1),
 "GRPDEALFLAG" VARCHAR(1),
 "GETMODE"  VARCHAR(2),
 "GETINTV"  SMALLINT,
 "CALFLAG"  VARCHAR(1),
 "UWFLAG"  VARCHAR(1),
 "DECLINEFLAG" VARCHAR(1),
 "ENDCASEFLAG" VARCHAR(1),
 "ENDCASEDATE" DATE,
 "MNGCOM"  VARCHAR(10) NOT NULL,
 "OPERATOR" VARCHAR(10) NOT NULL,
 "MAKEDATE" DATE  NOT NULL,
 "MAKETIME" VARCHAR(8) NOT NULL,
 "MODIFYDATE" DATE  NOT NULL,
 "MODIFYTIME" VARCHAR(8) NOT NULL,
 "BANKCODE" VARCHAR(10),
 "BANKACCNO" VARCHAR(40),
 "ACCNAME"  VARCHAR(120),
 "CASEGETMODE" VARCHAR(2),
 "ACCMODIFYREASON" VARCHAR(400),
 "CASENODATE" DATE,
 "CASEPROP" CHARACTER(2),
 "REMARK"  VARCHAR(2000),
 "CANCLEREASON" CHARACTER(2),
 "CANCLEREMARK" VARCHAR(500),
 "CANCLER"  CHARACTER(10),
 "CANCLEDATE" DATE,
 "RIGISTER" CHARACTER(10),
 "CLAIMER"  CHARACTER(10),
 "UWER"  CHARACTER(10),
 "UWDATE"  DATE,
 "SIGNER"  CHARACTER(10),
 "SIGNERDATE" DATE,
 "RISKCODE" VARCHAR(10),
 "OTHERIDTYPE" VARCHAR(2),
 "OTHERIDNO" VARCHAR(50),
 "GRPNO"  VARCHAR(20),
 "GRPNAME"  VARCHAR(150),
 "CONTDEALFLAG" VARCHAR(1),
 "MOBILEPHONE" VARCHAR(15),
 "PREPAIDFLAG" VARCHAR(1),
 PRIMARY KEY("CASENO")
 );


-- End of generated script for 10.136.1.5-DB2-LIS (db2inst1)
View Code

对应python语句:

技术分享
 1 import json
 2 
 3 def cleanSql(lines):
 4 list=[]
 5 str =‘‘
 6 table_name=‘‘
 7 primary_key=‘‘
 8 for line in lines:
 9  if ‘CREATE TABLE‘ in line:
10  table_name=line[line.find(‘."‘)+1:line.rfind(‘"‘)]
11  if ‘PRIMARY KEY‘ in line:
12  primary_key=line[line.find(‘"‘)+1:line.rfind(‘"‘)]
13  str += line
14 str = str.lower()
15 if str.startswith(‘create table‘) is False:
16  str =str[str.index(‘create table‘):len(str)]
17 if ‘primary key‘ in str:
18  str=str[0:str.find(‘primary key‘)]
19 if ‘foreign key‘ in str:
20  str=str[0:str.find(‘foreign key‘)]
21 str= str[str.index(‘(‘)+1:]
22 if ‘not null‘ in str:
23  str=str.replace(‘not null‘,‘‘)
24  list.append(table_name)
25  list.append(primary_key.lower())
26  list.append(str)
27 return list
28 
29 def changeType(type):
30 stype=type
31 if ‘character‘ in type:
32  stype =‘string‘
33 if ‘date‘ in type:
34  stype= ‘string‘
35 if ‘decimal‘ in type:
36  stype= ‘string‘
37 return stype
38 
39 def get_json(list):
40 primary_key=list[1]
41 str =list[2]
42 lines= str.splitlines(False)
43 json_list=[]
44 for line in lines:
45 json_dict={}
46 name=line[line.find(‘"‘)+1:line.rfind(‘"‘)].strip()
47 type=line[line.rfind(‘"‘)+1:line.find(‘,‘)].strip()
48 type =changeType(type)
49 json_dict[‘type‘]=type
50 json_dict[‘name‘]=name
51 if primary_key==name:
52  json_dict[‘primary‘]=1
53 if len(name)==0:
54  continue
55  json_list.append(json_dict)
56 return json_list
57 
58 def load():
59 schema=open(‘C:/Users/Administrator.PC--20150529IGF/Desktop/data/data/lis/llcase/full/20170401_09_37_01/schema.sql‘)
60 sqlStr =schema.readlines(100000)
61 list=cleanSql(sqlStr)
62 
63 jsonStr =get_json(list)
64 jsonS =json.dumps(jsonStr)
65  print(jsonS)
66 load()
View Code

 

python:sql建表语句转换为json

标签:--   discount   cat   class   sage   int   iter   rtti   ace   

热心网友 时间:2022-05-01 23:26

如果datas是Python的原始数据,并且datas中没有非ascii码,可以使用如下语句转换为json格式:
import
json
json.mps(datas)
当datas中含有中文等非ascii字符时,可以使用如下参数:
json.mps(datas,
ensure_ascii=False)
如果想美化输出格式,可以使用indent参数:
json.mps(datas,
indent=4)
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
erp可以看评论地址吗 淘宝评论url是什么意思? 揭秘:码牌支付风控升级,背后真相揭秘 电脑电视直播软件哪个好用什么软件好电脑看电视直播 潼南子同街学区是哪些 三极管BU406价格和参数? 火锅料放在冰柜忘了插电一个星期给会坏了吗 火锅的设备有哪些 火锅餐厅厨房有哪些 小火锅厨房设备有哪些 如何用python读取json里面的值啊 python3.0怎么用json从文件解析 怎么使用python提取json文件中的字段 “入”字的笔顺怎么写? 如何安装json包python3.5.2 入怎么写田字格 python3 默认有json库吗 “入”字的笔顺是什么? python 怎么提取json中的内容 入字在田字格正确写法 python导入Json库一直失败怎么解决 入字开头的成语有什么? 如何用python读取json文件里指定的数据 入可以组什么词 入字组词语有哪些 入的部首是什么 入口的入怎么写 python json怎么修改json数据 入字的意思是什么 如何用Python解析Json文件 如何搭建品牌私域流量体系? 品牌怎么做私域社群? 京东物流联手腾讯进军私域流量,私域流量特指的是... 私域流量,是个什么概念?流量池?怎么弄出来的? 怎么管理自己线上店铺的私域流量? 私域流量是什么意思? 私域流量是什么?如何构建并玩转私域流量呢? 私域流量实质上为企业带来了什么呢? 私域流量会是实体店商家的自救灵丹? 企业如何经营私域流量? 私域流量的核心是什么 知乎 私域的含义是什么? 发展私域流量有什么好处? 什么是私域流量?私域流量有什么作用? 私域流量运营平台哪家强? 私域流量这么火,实体店如何运营私域流量? 做护肤的,怎么运营私域流量? 河北的房地产是怎么去打造自己的私域流量的呢? 私域运营要怎么做? 律新社提供私域流量解决方案?