-
Notifications
You must be signed in to change notification settings - Fork 0
/
Excel to json.py
77 lines (58 loc) · 1.76 KB
/
Excel to json.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
import json as json
import os
import time
import xlrd
if __name__ == '__main__':
os.chdir(r"C:\Users\Aditya.Shetty\Desktop\Aditya\samples")
# By for logic
t1 = time.time()
wb = xlrd.open_workbook('t1.xlsx')
workbook = []
for x in range(0, wb.nsheets):
sheet = wb.sheet_by_index(x)
row_list = []
for rownum in range(1, sheet.nrows):
data = {}
row_values = sheet.row_values(rownum)
data["no"] = row_values[0]
data["name"] = row_values[1]
data["id"] = row_values[2]
data["age"] = row_values[3]
row_list.append(data)
sheetdata = {}
sheetdata[sheet.name] = row_list
workbook.append(sheetdata)
z = json.dumps({"t1": workbook})
with open("data.json", "w") as f:
f.write(z)
f.close()
print(z)
t2 = time.time()
print(t1, "\t", t2)
print("\ntime by for logic: ", t2 - t1)
print("\n")
'''
#By built in function
t3=time.time()
wb= xlrd.open_workbook('t1.xlsx')
#print(wb.nsheets)
#print(wb.sheets())
for x in wb.sheets():
data_list = []
for rownum in range(1, x.nrows):
data = OrderedDict()
row_values = x.row_values(rownum)
data["no"]= row_values[0]
data["name"]= row_values[1]
data["id"]= row_values[2]
data["age"]= row_values[3]
data_list.append(data)
j=json.dumps(data_list)
with open("data.json","a") as f:
f.write(j)
f.close()
print(j)
t4=time.time()
print(t3,"\t",t4)
print("\ntime by Built in function:",t4-t3)
'''