Python converts the json format to a text or SQL file,
How does python quickly convert data in json format into data in the specified format? Or convert to an SQL file?
In the following example, the data in json format is replaced with text data separated by # _ #, which can also be used to generate an SQL file.
[root@bogon tutorial]# vi json2txt.py #-*- coding: UTF-8 -*-import jsondata = []with open('./tencent_test.json') as f: for line in f: data.append(json.loads(line))#print json.dumps(data, ensure_ascii=False)import codecsfile_object = codecs.open('tencent.txt', 'w' ,"utf-8")str = "\r\n"splitstr = "#_#"for item in data: #print json.dumps(item) #str = str + "insert into tencent(name,catalog,workLocation,recruitNumber,detailLink,publishTime) values " #str = str + "'%s','%s','%s','%s','%s'\r\n" % (item['parentTitle'],item['parentLink'],item['author'],item['link'],item['title']) #print json.loads(item['author']) + "\r\n" str = "%s#_#%s#_#%s#_#%s#_#%s\r\n" % (item['parentTitle'],item['parentLink'],item['author'],item['link'],item['title'].strip()) file_object.write(str)#import codecs#file_object = codecs.open('tencent.txt', 'w' ,"utf-8")#file_object.write(str)file_object.close()print "success"
Note: If the data volume is too large, it is not recommended that all files be written in str at a time. You can consider writing data row by row or writing data in batches to a certain extent.
In addition, python replaces strings with strip (rm). When rm is empty, blank spaces (including '\ n',' \ R', '\ t ', '').
Test:
Data in json format
[Root @ bogon tutorial] # vi tencent_test.json {"author": "author", "parentTitle": "parent title", "title": "title", "pageUrl ": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title ", "title": "title", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl" }{ "author ": "author", "parentTitle": "parent title", "title": "title", "pageUrl": "pageurl", "link": "linkurl ", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title", "title": "title", "pageUrl ": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title ", "title": "title", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl" }{ "author ": "author", "parentTitle": "parent title", "title": "title", "pageUrl": "pageurl", "link": "linkurl ", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title", "title": "title", "pageUrl ": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title ", "title": "title", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl" }{ "author ": "author", "parentTitle": "parent title", "title": "title", "pageUrl": "pageurl", "link": "linkurl ", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title", "title": "title", "pageUrl ": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "author", "parentTitle": "parent title ", "title": "title", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl "}
Run python json2txt. py
[Root @ bogon tutorial] # python json2txt. py success [root @ bogon tutorial] # more tencent.txt parent title #_# parenturl #_# author #_# linkurl #_# title parent title #_# parenturl #_# author # _ # linkurl #### title parent title ###parenturl ###author ##linkurl ### title parent title ###parenturl ###author ### linkurl # _ # title parent title ###parenturl ###author ###linkurl ### title parent title ###parenturl ###author ### linkurl ### title parent title ### parenturl ###author #### linkurl ### title parent title ###parenturl ###author ### linkurl ### title parent title #_# parenturl ### author #### linkurl ### title parent title ###parenturl ###author ### linkurl #### title parent title ### parenturl #_# author ### linkurl ### title