forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0015.py
More file actions
34 lines (29 loc) · 774 Bytes
/
Copy path0015.py
File metadata and controls
34 lines (29 loc) · 774 Bytes
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
#!/bin/env python
# -*- coding: utf-8 -*-
#导入模块
import simplejson as json
import xlwt
#从文件(JSON形式)中读取数据返回字典
def read_file(filename):
with open(filename,'r') as fp:
content = fp.read()
d = json.JSONDecoder().decode(content)
return d
#生成对应的xls文件
def gen_xls(d,filename):
fp = xlwt.Workbook()
table = fp.add_sheet('city',cell_overwrite_ok=True)
#这次一次循环就可以了
for n in range(len(d)):
table.write(n,0,n+1)
table.write(n,1,d[str(n+1)])
fp.save('city.xls')
print '写入完毕'
#主函数
def main():
filename = 'city.txt'
xls_name = 'city.xls'
d = read_file(filename)
gen_xls(d,xls_name)
if __name__ == '__main__':
main()