Python converts the json format to a text or SQL file,

Source: Internet
Author: User

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

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.