-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel2json.py
687 lines (592 loc) · 15.9 KB
/
excel2json.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 这段代码主要的功能是把excel表格转换成utf-8格式的json文件
# lastdate:2011-8-15 14:21 version 1.1
"""
https://github.com/zdhsoft/excel2json
pip install pyinstaller
pyinstaller -F excel2json.py
"""
import os
import sys
import codecs
import json
import xlrd #http://pypi.python.org/pypi/xlrd
from collections import OrderedDict
from enum import Enum
import types
reload(sys) # reload 才能调用 setdefaultencoding 方法
sys.setdefaultencoding('utf-8') # 设置 'utf-8'
def space_str(layer):
lua_str = ""
for i in range(0,layer):
lua_str += '\t'
return lua_str
def obj_to_lua_str(data, layer=0):
d_type = type(data)
if d_type is types.ListType:
lua_str = "{\n"
lua_str += space_str(layer+1)
for i in range(0,len(data)):
lua_str += ("[" + str(i+1) + "] = ")
lua_str += dic_to_lua_str(data[i],layer+1)
if i < len(data)-1:
lua_str += ','
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
elif d_type is types.DictType:
lua_str = ''
lua_str += "{\n"
data_len = len(data)
data_count = 0
for k,v in data.items():
data_count += 1
lua_str += space_str(layer+1)
if type(k) is types.IntType:
lua_str += '[' + str(k) + ']'
else:
lua_str += '["' + str(k) + '"]'
lua_str += ' = '
try:
lua_str += dic_to_lua_str(v,layer +1)
if data_count < data_len:
lua_str += ',\n'
except Exception, e:
print 'error in ',k,v
raise
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
else:
print d_type , 'is error'
return None
def dic_to_lua_str(data,layer=0):
d_type = type(data)
if d_type is types.NoneType:
return 'nil'
elif d_type is types.StringTypes or d_type is str or d_type is types.UnicodeType:
return "'" + data + "'"
elif d_type is types.BooleanType:
if data:
return 'true'
else:
return 'false'
elif d_type is types.IntType or d_type is types.LongType or d_type is types.FloatType:
return str(data)
elif d_type is types.ListType:
lua_str = "{\n"
lua_str += space_str(layer+1)
for i in range(0,len(data)):
lua_str += ("[" + str(i+1) + "] = ")
lua_str += dic_to_lua_str(data[i],layer+1)
if i < len(data)-1:
lua_str += ','
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
elif d_type is types.DictType:
lua_str = ''
lua_str += "\n"
lua_str += space_str(layer)
lua_str += "{\n"
data_len = len(data)
data_count = 0
for k,v in data.items():
data_count += 1
lua_str += space_str(layer+1)
if type(k) is types.IntType:
lua_str += '[' + str(k) + ']'
else:
lua_str += '["' + str(k) + '"]'
lua_str += ' = '
try:
lua_str += dic_to_lua_str(v,layer +1)
if data_count < data_len:
lua_str += ',\n'
except Exception, e:
print 'error in ',k,v
raise
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
else:
print d_type , 'is error'
return None
# 数据类型枚举
class DataType(Enum):
NONE = 0
INT = 1
FLOAT = 2
BOOL = 3
STRING = 4
OBJECT = 5
ANY = 6
def IsJson(strValue):
try:
obj = json.loads(strValue, encoding='utf-8')
d_type = type(obj)
if d_type is types.ListType:
return True
elif d_type is types.DictType:
return True
return False
except ValueError:
return False
return False
def IsInt(strValue):
try:
int(strValue)
except ValueError:
return False
return True
def IsFloat(strValue):
try:
float(strValue)
except ValueError:
return False
return True
AcessDataType = {'INT': DataType.INT, 'FLOAT': DataType.FLOAT, 'BOOL': DataType.BOOL, 'STRING': DataType.STRING, 'OBJECT': DataType.OBJECT, 'ANY': DataType.ANY}
def FloatToString (aFloat):
if type(aFloat) != float:
return u""
strTemp = str(aFloat)
strList = strTemp.split(".")
if len(strList) == 1 :
return strTemp
else:
if strList[1] == "0" :
return u""+strList[0]
else:
return u""+strTemp
def ParseExtType(paramExtType):
strList = paramExtType.split(":")
retType = {}
if len(strList) == 2:
retType["key"] = strList[0]
retType["type"] = strList[1]
else:
retType["key"] = ""
retType["type"] = ""
return retType
#查找第1个非要求的字符串的下标
def findFirstNot(str, begin, substr):
for i in range(begin, len(str)):
if substr.find(str[i]) == -1:
return i
return -1
#解析filter字符串,返回变量数组
def parseFilterKey(filter):
ret = []
begin = 0
while True:
index = filter.find("$", begin)
if index >= 0:
index += 1
end = findFirstNot(filter, index, "1234567890abcdefghijklmnopqrstuvwxyz_ABC DEFGHIJKLMNOPQRSTUVWXYZ")
key = filter[index:end]
ret.append(key)
begin = end
else:
return ret
#读入字段映射表
def readMap(table):
mapTable = {}
nrow = table.nrows
if table.ncols == 0:
return mapTable
for r in range(nrow):
k = table.cell_value(r, 0)
if table.ncols < 2:
v = k
else:
v = table.cell_value(r, 1)
if (len(v) == 0):
v = k
mapTable[k] = v
return mapTable
#读取字段列表
def readFieldMap(paramFields):
mapField = {}
strList = paramFields.split(",")
for f in strList:
strNameList = f.split(":")
if len(strNameList) > 1:
mapField[strNameList[0]]=strNameList[1]
else:
mapField[f] = f
return mapField
# table2as3config(destTable, destFileName, mapTable, mapParam)
def CellToString(paramCell):
strCellValue = u""
if type(paramCell) == unicode:
strCellValue = paramCell
elif type(paramCell) == float:
strCellValue = FloatToString(paramCell)
else:
strCellValue = str(paramCell)
return strCellValue
def IsEmptyLine(paramTable, paramRow, paramFieldCount):
linecnt = 0
for i in range(paramFieldCount-1):
v = paramTable.cell_value(paramRow, i)
if type(v) == unicode:
v = v
elif type(v) == float:
v = FloatToString(v)
else:
v = str(v)
linecnt += len(v)
if linecnt > 0:
return False
if linecnt == 0:
return True
else:
return False
# 数据解析
def DealData(dataType, data):
if data == "null":
return True, data
ok = False
strValue = u""
if dataType == DataType.INT:
# 这里读进来的整形 带.0
if type(data) != float:
return ok, strValue
strValue = FloatToString(data)
if IsInt(strValue) == False:
return ok, strValue
strValue = str(strValue)
elif dataType == DataType.FLOAT:
if type(data) != float:
return ok, strValue
strValue = FloatToString(data)
elif dataType == DataType.BOOL:
if data == 0:
strValue = u"false"
elif data == 1:
strValue = u"true"
else:
return ok, strValue
elif dataType == DataType.STRING:
if type(data) == float:
data = FloatToString(data)
if type(data) != unicode:
return ok, strValue
#strValue = data.replace(u"\\", u"\\\\").replace(u"\"", u"\\\"")
#strValue = strValue.replace(u"\n", u"")
strValue = "\""+ data + u"\""
elif dataType == DataType.OBJECT:
obj = data.replace(u"\\", u"")
if IsJson(obj) == False:
return ok, strValue
strValue = obj
elif dataType == DataType.ANY:
while 1:
ok, strValue = DealData(DataType.INT, data)
if ok == True:
break
ok, strValue = DealData(DataType.FLOAT, data)
if ok == True:
break
ok, strValue = DealData(DataType.BOOL, data)
if ok == True:
break
ok, strValue = DealData(DataType.OBJECT, data)
if ok == True:
break
#ok, strValue = DealData(DataType.STRING, data)
#if ok == True:
# break
#return ok, strValue
return True, data
else:
return ok, strValue
return True, strValue
def table2array(table, filename, suffix, filter, mapTable, desc):
nrows = table.nrows
ncols = table.ncols
hasMap = (len(mapTable) > 0)
# 头部加上"[\n"
json_str = u"[\n"
rs = 0
for r in range(2, nrows):
if IsEmptyLine(table, r, ncols): #跳过空行
continue
strTmp = u"\t{ "
i = 0
# 过滤不需要的行
if len(filter) > 0:
dic = {}
for c in range(ncols):
key = table.cell_value(0,c)
value = table.cell_value(r,c)
dic[key] = value
if (eval(filter,{},dic)) == False:
continue
for c in range(ncols):
# 根据列名校验是否是要导出的列
title = table.cell_value(0,c)
title = title.replace(u"\n", u"").replace(u"\"", u"")
if hasMap:
if not title in mapTable:
continue
else:
title = mapTable[title]
# 校验列的数据类型
colType = table.cell_value(1,c)
if not colType in AcessDataType:
print (u"data type error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
dataType = AcessDataType[colType]
# 取数据
data = table.cell_value(r,c)
# 数据校验转换
ok, strValue = DealData(dataType, data)
if ok == False:
print (u"data format error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
if i > 0:
strTmp += u", " + u"\"" + title + u"\":"+ strValue
else:
strTmp += u"" + u"\"" + title + u"\":"+ strValue
i += 1
# 当前行结束
strTmp += u" }"
if rs > 0: #不是第1行
strTmp = ",\n" + strTmp
rs += 1
json_str += strTmp
# 尾部加上"\n]"
json_str += u"\n]"
json_str += u"\n"
# 打开输出文件
dir = os.path.dirname(filename)
if dir and not os.path.exists(dir):
os.makedirs(dir)
f = codecs.open(filename,"w","utf-8")
if suffix == ".json":
f.write(json.dumps(json.loads(json_str, object_pairs_hook=OrderedDict),ensure_ascii=False,indent=4).replace(u"\\\\", u"\\"))
elif suffix == ".lua":
if len(desc) > 0:
f.write("--[[\n")
f.write(str(desc))
f.write("\n]]--\n")
f.write("return " + obj_to_lua_str(json.loads(json_str)))
f.close()
print "Create ",filename," OK"
return
def table2map(table, filename, suffix, filter, mapTable, desc):
nrows = table.nrows
ncols = table.ncols
hasMap = (len(mapTable) > 0)
json_str = u"{\n"
keyIndex = 0
rs = 0
for r in range(2, nrows):
if IsEmptyLine(table, r, ncols): #跳过空行
continue
i = 0
keyValue = table.cell_value(r,keyIndex)
if type(keyValue) == unicode:
keyValue = keyValue
elif type(keyValue) == float:
keyValue = FloatToString(keyValue)
else:
keyValue = str(keyValue)
strTmp = u"\t\""+str(keyValue) + "\": { "
# 过滤不需要的行
if len(filter) > 0:
dic = {}
for c in range(ncols):
key = table.cell_value(0,c)
value = table.cell_value(r,c)
dic[key] = value
if (eval(filter,{},dic)) == False:
continue
for c in range(ncols):
# 根据列名校验是否是要导出的列
title = table.cell_value(0,c)
title = title.replace(u"\n", u"").replace(u"\"", u"")
if hasMap:
if not title in mapTable:
continue
else:
title = mapTable[title]
# 校验列的数据类型
colType = table.cell_value(1,c)
if not colType in AcessDataType:
print (u"data type error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
dataType = AcessDataType[colType]
# 取数据
data = table.cell_value(r,c)
# 数据校验转换
ok, strValue = DealData(dataType, data)
if ok == False:
print (u"data format error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
if i > 0:
delm = u", "
else:
delm = u""
strTmp += delm + u"\"" + title + u"\":"+ strValue
i += 1
strTmp += u" }"
if rs > 0: #不是第1行
strTmp = u",\n" + strTmp
rs += 1
json_str += strTmp
json_str += u"\n}"
json_str += u"\n"
# 打开输出文件
dir = os.path.dirname(filename)
if dir and not os.path.exists(dir):
os.makedirs(dir)
f = codecs.open(filename,"w","utf-8")
if suffix == ".json":
f.write(json.dumps(json.loads(json_str, object_pairs_hook=OrderedDict),ensure_ascii=False,indent=4).replace(u"\\\\", u"\\"))
elif suffix == ".lua":
if len(desc) > 0:
f.write("--[[\n")
f.write(str(desc))
f.write("\n]]--\n")
f.write("return " + obj_to_lua_str(json.loads(json_str)))
f.close()
print "Create ",filename," OK"
return
def table2keyvalue(table, filename, suffix, filter, mapTable, desc):
nrows = table.nrows
ncols = table.ncols
hasMap = (len(mapTable) > 0)
json_str = u"{\n"
keyIndex = 0
rs = 0
for r in range(2, nrows):
if IsEmptyLine(table, r, ncols): #跳过空行
continue
i = 0
# 过滤不需要的行
if len(filter) > 0:
dic = {}
for c in range(ncols):
key = table.cell_value(0,c)
value = table.cell_value(r,c)
dic[key] = value
if (eval(filter,{},dic)) == False:
continue
keyValue = table.cell_value(r,keyIndex)
if type(keyValue) == unicode:
keyValue = keyValue
elif type(keyValue) == float:
keyValue = FloatToString(keyValue)
else:
keyValue = str(keyValue)
strTmp = u"\t\""+keyValue + "\":"
for c in range(ncols):
# 根据列名校验是否是要导出的列
title = table.cell_value(0,c)
title = title.replace(u"\n", u"").replace(u"\"", u"")
if hasMap:
if not title in mapTable:
continue
else:
title = mapTable[title]
# 校验列的数据类型
colType = table.cell_value(1,c)
if not colType in AcessDataType:
print (u"data type error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
dataType = AcessDataType[colType]
# 取数据
data = table.cell_value(r,c)
# 数据校验转换
ok, strValue = DealData(dataType, data)
if ok == False:
print (u"data format error on make ", filename, "row:", r, "col:", c, "title:", title)
sys.exit(1)
if i == 1:
strTmp += u""+ strValue
i += 1
if i >= 2:
break
# strTmp += u" }"
if rs > 0: #不是第1行
strTmp = u",\n" + strTmp
rs += 1
json_str += strTmp
json_str += u"\n}"
json_str += u"\n"
# 打开输出文件
dir = os.path.dirname(filename)
if dir and not os.path.exists(dir):
os.makedirs(dir)
f = codecs.open(filename,"w","utf-8")
if suffix == ".json":
f.write(json.dumps(json.loads(json_str, object_pairs_hook=OrderedDict),ensure_ascii=False,indent=4).replace(u"\\\\", u"\\"))
elif suffix == ".lua":
if len(desc) > 0:
f.write("--[[\n")
f.write(str(desc))
f.write("\n]]--\n")
f.write("return " + obj_to_lua_str(json.loads(json_str)))
f.close()
print "Create ",filename," OK"
return
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: %s <excel_file>' % sys.argv[0]
sys.exit(1)
print "handle file: %s" % sys.argv[1]
excelFileName = sys.argv[1]
# excelFileName = "config.xlsx"
# 打开excel表
data = xlrd.open_workbook(excelFileName)
# 读取 tablelist 导出配置页
table = data.sheet_by_name(u"tablelist")
for r in range(table.nrows-1):
# 读目的table名(filename)
destTableName = table.cell_value(r+1,0)
# 读输出文件名(outfilename)
destFileName = table.cell_value(r+1,2)
s = "undefined"
# 读导出的列(fields)
strUseFields = u""
if (table.ncols >= 4):
strUseFields = table.cell_value(r+1,3)
mapTable = readFieldMap(strUseFields)
# 读出行筛选条件
strFilter = table.cell_value(r+1,4)
# 读导出类型(type)
strExtType = u""
if(table.ncols >= 6):
strExtType = table.cell_value(r+1, 5)
retType = ParseExtType(strExtType)
print strExtType, retType, retType["key"], retType["type"]
# 读出描述
strDesc = table.cell_value(r+1,6)
print "\nCreate " + destTableName + " ==> " + destFileName + " Starting..."
# 读取目的table数据
destTable = data.sheet_by_name(destTableName)
# 取目标文件后缀名
suffix = destFileName[destFileName.rfind("."):].lower()
#if suffix == ".json" or suffix == ".lua":
if suffix == ".json" or suffix == ".lua":
if retType["type"] == "array":
table2array(destTable, destFileName, suffix, strFilter, mapTable, strDesc)
elif retType["type"] == "map":
table2map(destTable, destFileName, suffix, strFilter, mapTable, strDesc)
elif retType["type"] == "value":
table2keyvalue(destTable, destFileName, suffix, strFilter, mapTable, strDesc)
else:
print u"type only support key:array,key:map,key:value format " + destFileName
sys.exit(1)
else:
print u"当前类型是:", suffix
print u"only support (json, lua), conf format " + destFileName
sys.exit(1)
print "-------- All OK --------"