Skip to content

Commit 380c769

Browse files
committed
complete 0015
1 parent 187d7f1 commit 380c769

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Jimmy66/0015/0015.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+
d = json.JSONDecoder().decode(content)
13+
return d
14+
15+
#生成对应的xls文件
16+
def gen_xls(d,filename):
17+
fp = xlwt.Workbook()
18+
table = fp.add_sheet('city',cell_overwrite_ok=True)
19+
#这次一次循环就可以了
20+
for n in range(len(d)):
21+
table.write(n,0,n+1)
22+
table.write(n,1,d[str(n+1)])
23+
fp.save('city.xls')
24+
print '写入完毕'
25+
26+
#主函数
27+
def main():
28+
filename = 'city.txt'
29+
xls_name = 'city.xls'
30+
d = read_file(filename)
31+
gen_xls(d,xls_name)
32+
33+
if __name__ == '__main__':
34+
main()

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

Jimmy66/0015/city.xls

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)