File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ {
2
+ "1" : "上海",
3
+ "2" : "北京",
4
+ "3" : "成都"
5
+ }
You can’t perform that action at this time.
0 commit comments