JSON與CSV的轉換

思路:

先把多個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 json
import csv
import fcntl
import 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=[]
# 存儲所有要讀取的KEY
all_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()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章