Skip to content

Commit a891111

Browse files
committed
complete 0016
1 parent 380c769 commit a891111

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Jimmy66/0016/0016.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
#导入模块
5+
import simplejson as json
6+
import xlwt
7+
8+
#从文件(JSON形式)中读取数据返回列表
9+
def read_file(filename):
10+
with open(filename,'r') as fp:
11+
content = fp.read()
12+
l = json.JSONDecoder().decode(content)
13+
return l
14+
15+
#生成对应的xls文件
16+
def gen_xls(l,filename):
17+
fp = xlwt.Workbook()
18+
table = fp.add_sheet('numbers',cell_overwrite_ok=True)
19+
#列表的遍历,用index真的好方便,内存循环踩了个坑,要改用row而不是l作为索引对象
20+
for row in l:
21+
for col in row:
22+
table.write(l.index(row),row.index(col),col) #row表示行,col表示列,后者的英文不一定匹配,我还是再查找下,list.index()可以得到列表中对于元素的索引值
23+
fp.save('numbers.xls')
24+
print '写入完毕'
25+
26+
#主函数,我猜这次返回的应该是列表吧
27+
def main():
28+
filename = 'numbers.txt'
29+
xls_name = 'numbers.xls'
30+
l = read_file(filename)
31+
gen_xls(l,xls_name)
32+
33+
if __name__ == '__main__':
34+
main()

Jimmy66/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+
]

Jimmy66/0016/numbers.xls

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)