How does python quickly translate JSON-formatted data into data in a given format? or convert it to a SQL file?
The following example converts a JSON-formatted data into a #_#-segmented text data and can also be used to generate a 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 amount of data is too large, it should not be put in Str to write files once. You can consider writing on a line-by-row basis, or bulk writing to a certain extent.
In addition: Python replaces the string with strip (RM), and when RM is empty, the default is to remove whitespace (including ' \ n ', ' \ R ', ' \ t ', ').
Test:
JSON-formatted data
[Root@bogon tutorial]# VI Tencent_test.json {"Author": "Author", "Parenttitle": "Parent title", "title": "title", "Pageurl": "Pageurl", "l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" Linkurl "," Parentlink ":" ParentURL "} {" Author ":" Author "," Parenttitle ":" Parent title "," title ":" title "," Pageurl ":" Pageurl "," l Ink ":" 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