思路:
先把多个JSON元素转换成数组,然后使用python的csv和json模块来处理。
范例格式:
[{ "description": null, "p_w_picpath": { "p_w_picpath_size": 20, "os_family": "centos", "platform": "linux", "p_w_picpath_name": "tomcat-V1.7-基础镜像-V2", "provider": "self", "p_w_picpath_id": "xxxxxxxxx", "processor_type": "64bit", "ui_type": "tui" }, "instance_name": "幽幽", "transition_status": "", "sub_code": 0, "lastest_snapshot_time": "", "cpu_topology": "", "memory_current": 4096, "vxnets": [], "status": "ceased", "vcpus_current": 2, "instance_id": "i-xxxxxx", "instance_type": "c2m4", "instance_class": 0, "dns_aliases": [], "create_time": "2015-03-17T02:06:57Z", "owner": "xxxxxxxxxxxx", "status_time": "2015-03-17T04:14:49Z", "alarm_status": "" }]
使用方法:
python json2csv.py /tmp/test.json /tmp/test.csv instance_id,instance_name,create_time,vcpus_current,memory_current,status,status_time p_w_picpath
处理结果如下:
i-xxxxxx,xxxxxx,2015-03-17T02:18:34Z,2,4096,ceased,2015-03-17T04:22:04Z,tui,64bit,xxxxxxxxxxxxxxxxxx,20,xxxxxxxx,linux,centos,self
直接上代码
#!/usr/bin/env python# -*- coding: utf-8 -*-import jsonimport csvimport fcntlimport time import sys reload(sys) sys.setdefaultencoding('utf8')# 读文件句柄f_read = open(sys.argv[1])data = json.load(f_read)f_read.close()# 写文件句柄f_write=open(sys.argv[2],'wb+')f=csv.writer(f_write)# 文件锁fcntl.flock(f_write,fcntl.LOCK_EX)# 读接下来的变量del sys.argv[0]del sys.argv[0]del sys.argv[0]# 层级存储要读取的KEY到列表中if len(sys.argv)>0: m1=sys.argv[0].split(',') del sys.argv[0]else: m1=[]if len(sys.argv)>0: m2=sys.argv[0].split(',') del sys.argv[0]else: m2=[]if len(sys.argv)>0: m3=sys.argv[0].split(',') del sys.argv[0]else: m3=[]# 存储所有要读取的KEYall_keys=m1+m2+m3# 打印表头#print ','.join(all_keys)#f.writerow(all_keys) # 逐一打印元素for item in data: x=[] for z in all_keys: if z in m1: x+=[str(item[z]).encode('utf-8', 'ignore')] if z in m2: x+=item[z].values() #print ','.join(x) f.writerow(x)fcntl.flock(f_write,fcntl.LOCK_UN)f_write.close()