Python實現把json格式轉換成文本或sql檔案

來源:互聯網
上載者:User
python如何將json格式的資料快速的轉化成指定格式的資料呢?或者轉換成sql檔案?

下面的例子是將json格式的資料准換成以#_#分割的文本資料,也可用於產生sql檔案。

[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"

註:如果資料量過大不宜全部放在str裡一次性寫入檔案。可以考慮逐行寫入,或者到達一定程度時批量寫入。

另外:python通過strip(rm)替換字串,當rm為空白時,預設刪除空白符(包括'\n', '\r', '\t', ' ')。

測試:

json格式的資料

[root@bogon tutorial]# vi tencent_test.json {"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}{"author": "作者", "parentTitle": "父標題", "title": "標題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}

運行python json2txt.py

[root@bogon tutorial]# python json2txt.py success[root@bogon tutorial]# more tencent.txt 父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題父標題#_#parenturl#_#作者#_#linkurl#_#標題
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.