Python实现把json格式转换成⽂本或sql⽂件python如何将json格式的数据快速的转化成指定格式的数据呢?或者转换成sql⽂件?
下⾯的例⼦是将json格式的数据准换成以#_#分割的⽂本数据,也可⽤于⽣成sql⽂件。
documentary的中文[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('', 'w' ,"utf-8")
powerful camerastr = "\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('', 'w' ,"utf-8")
#file_object.write(str)
file_object.close()
division近义词print "success"
注:如果数据量过⼤不宜全部放在str⾥⼀次性写⼊⽂件。可以考虑逐⾏写⼊,或者到达⼀定程度时批量写⼊。另外:python通过strip(rm)替换字符串,当rm为空时,默认删除空⽩符(包括'\n', '\r', '\t', ' ')。
测试:
json格式的数据
[root@bogon tutorial]# vi tencent_test.jsonpython解析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.py11月28日cnn
[root@bogon tutorial]# python json2txt.py
success
[root@bogon tutorial]#
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题layuiadmin和layui区别
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
⽗标题#_#parenturl#_#作者#_#linkurl#_#标题⽗标题#_#parenturl#_#作者#_#linkurl#_#标题
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论