Skip to content

Commit 647392c

Browse files
author
chris
committed
add ex0015 and ex0016
1 parent b88d811 commit 647392c

6 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎GitRangerLiu/0015/city.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1" : "�Ϻ�",
3+
"2" : "����",
4+
"3" : "�ɶ�"
5+
}

‎GitRangerLiu/0015/city.xls‎

5.5 KB
Binary file not shown.

‎GitRangerLiu/0015/json_to_excel.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
import xlwt
4+
5+
testfile = 'city.txt'
6+
savename = 'city.xls'
7+
sheetname = 'big city'
8+
9+
def json_to_excel():
10+
info = get_json(testfile)
11+
workbook = xlwt.Workbook()
12+
sheet = workbook.add_sheet(sheetname)
13+
14+
print info
15+
row_total = len(info)
16+
col_total = 1
17+
#The keys of dict is special, so we just use 1, 2, 3
18+
for r in range(row_total):
19+
sheet.write(r, 0, r + 1)
20+
21+
for r in range(row_total):
22+
for c in range(col_total):
23+
#Get values by the key
24+
val = info[str(r + 1)]
25+
sheet.write(r, c + 1, val)
26+
workbook.save(savename)
27+
28+
#Convert json format file to python object
29+
def get_json(testfile):
30+
with open(testfile, 'r') as f:
31+
#The codec should be the same as the file encoding.
32+
text = f.read().decode('GB2312')
33+
return json.loads(text)
34+
35+
if __name__ == '__main__':
36+
json_to_excel()

‎GitRangerLiu/0016/json_to_excel.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
import json
3+
import xlwt
4+
5+
testfile = 'numbers.txt'
6+
savename = 'numbers.xls'
7+
sheetname = 'number'
8+
9+
def json_to_excel():
10+
info = get_json(testfile)
11+
workbook = xlwt.Workbook()
12+
sheet = workbook.add_sheet(sheetname)
13+
14+
print info
15+
row_total = len(info)
16+
col_total = len(info[0])
17+
18+
for r in range(row_total):
19+
for c in range(col_total):
20+
val = info[r][c]
21+
sheet.write(r, c, val)
22+
workbook.save(savename)
23+
24+
#Convert json format file to python object
25+
def get_json(testfile):
26+
with open(testfile, 'r') as f:
27+
#The codec should be the same as the file encoding.
28+
text = f.read().decode('GB2312')
29+
return json.loads(text)
30+
31+
if __name__ == '__main__':
32+
json_to_excel()

‎GitRangerLiu/0016/numbers.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
[1, 82, 65535],
3+
[20, 90, 13],
4+
[26, 809, 1024]
5+
]

‎GitRangerLiu/0016/numbers.xls‎

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)