-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonToCsv.py
156 lines (144 loc) · 4.55 KB
/
JsonToCsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python
# _*_coding:utf-8 _*_
import datetime
import json
import sys
import time
reload(sys)
sys.setdefaultencoding('utf8')
plusfilename = []
#示例
bodystat = {
"reqStatus": {
"1": "需求提出",
"21": "部门审核",
"22": "分公司办结",
"31": "需求受理",
"41": "架构初评",
"51": "委员会初审",
"52": "立项中",
"61": "需求分析",
"71": "架构设计",
"81": "需求排期",
"91": "解决方案阅知",
"101": "需求评审",
"111": "业务确认",
"121": "部门确认",
"131": "委员会终审",
"141": "需求实施",
"151": "需求完成",
"161": "需求取消",
"171": "需求暂停",
"181": "审批中"},
"reqType": {
"1": "功能类",
"2": "配置类",
"3": "数据提取",
"5": "新项目类",
"7": "技术优化类"
}
}
#示例
title = {
"systemName": "系统名称",
"systemCode": "系统代码",
"reqName": "需求名称",
"reqCode": "需求代码",
"submitterName": "提交人",
"reqType": "需求类型",
"reqStatus": "需求状态",
"createDate": "创建时间",
"planRealeaseTime": "计划上线时间",
"orderStatusStore": "订单状态存储",
"orderStatus": "订单状态",
"orderCode": "订单代码",
"actId": "不知道什么意思",
"dealMan": "当前处理人"
}
def checkjson(dict1, filepath):
for masterline in dict1.keys():
key = masterline.encode('utf-8')
# print key
typestr = type(dict1[key])
if typestr == list:
listprint(dict1[key], filepath)
elif typestr == dict:
checkjson(dict1[key], filepath)
def fileread(filepath):
result = ""
for line in open(filepath):
result = result + line
try:
dict1 = json.loads(result)
checkjson(dict1, filepath)
except Exception as e:
print e.message
def listprint(lists, filepath):
keys1 = set()
for line in lists:
for key in line.keys():
keys1.add(key.encode('utf-8'))
keys = []
lines = ""
for key in keys1:
keys.append(key)
if title.has_key(key):
lines = lines + '"' + title[key.encode('utf-8')] + '",'
else:
lines = lines + '"' + key.encode('utf-8') + '",'
filename = filepath.split('.')[0] + " " + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') + ".csv"
plusfilename.append(filename)
with open(filename, "w") as fo:
#utf-8 head
fo.write('\xEF\xBB\xBF')
fo.write(lines[0:len(lines) - 1] + '\n')
for line in lists:
lines = ''
for key in keys:
value = 'NULL'
if line.has_key(key):
value = str(line[key])
if value.find('"'):
value.replace('"', '""')
valuelength = len(value)
if value.isdigit():
if valuelength == 10:
try:
value = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(value)))
except Exception as e:
pass
if valuelength == 13:
try:
value = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(value) / 1000))
except Exception as e:
pass
if bodystat.has_key(key) and bodystat[key].has_key(value):
lines = lines + '"' + bodystat[key][value] + '",'
else:
lines = lines + '"' + value + '",'
fo.write(lines[0:len(lines) - 1] + '\n')
print filepath + ' finish'
fo.close()
if __name__ == '__main__':
if len(sys.argv) >= 2:
i = 0
for name in sys.argv:
if i != 0:
print sys.argv[i]
fileread(sys.argv[i])
i = i + 1
filename = ''
type = 'w'
for key in plusfilename:
if filename == '':
filename = key.split('.')[0] + " " + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') + ".csv"
else:
type = 'a'
control = 0
with open(filename, type) as fo1:
with open(key, 'r') as fo2:
if control == 0 and type == 'a':
fo2.readline()
control = 1
fo1.writelines(fo2.readlines())
print (filename+" finish")