Python implementation converts JSON format to text or SQL file

Source: Internet
Author: User
Tags parentlink
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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.