圆月山庄资源网 Design By www.vgjia.com

python如何将json格式的数据快速的转化成指定格式的数据呢?或者转换成sql文件?

下面的例子是将json格式的数据准换成以#_#分割的文本数据,也可用于生成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 = "\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#_#标题

标签:
Python,json格式,转换,文本,sql文件

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com