下面的例子是將json格式的數(shù)據(jù)準(zhǔn)換成以#_#分割的文本數(shù)據(jù),也可用于生成sql文件。
[root@bogon tutorial]# vi json2txt.py #-*- coding: UTF-8 -*- import json data = [] with open('./tencent_test.json') as f: for line in f: data.append(json.loads(line)) #print json.dumps(data, ensure_ascii=False) import codecs file_object = codecs.open('tencent.txt', 'w' ,"utf-8") str = " " 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' " % (item['parentTitle'],item['parentLink'],item['author'],item['link'],item['title']) #print json.loads(item['author']) + " " str = "%s#_#%s#_#%s#_#%s#_#%s " % (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"
注:如果數(shù)據(jù)量過(guò)大不宜全部放在str里一次性寫(xiě)入文件??梢钥紤]逐行寫(xiě)入,或者到達(dá)一定程度時(shí)批量寫(xiě)入。
另外:python通過(guò)strip(rm)替換字符串,當(dāng)rm為空時(shí),默認(rèn)刪除空白符(包括' ', ' ', ' ', ' ')。
測(cè)試:
json格式的數(shù)據(jù)
[root@bogon tutorial]# vi tencent_test.json {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"} {"author": "作者", "parentTitle": "父標(biāo)題", "title": "標(biāo)題", "pageUrl": "pageurl", "link": "linkurl", "parentLink": "parenturl"}
運(yùn)行python json2txt.py
[root@bogon tutorial]# python json2txt.py success [root@bogon tutorial]# more tencent.txt 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題 父標(biāo)題#_#parenturl#_#作者#_#linkurl#_#標(biāo)題
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com