File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+ # coding=utf-8
3+
4+ """
5+ 第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示,
6+ 请将上述内容写到 student.xls 文件中,如下图所示:
7+ """
8+
9+ import os
10+ import json
11+ import xlwt
12+
13+ def read_txt (path ):
14+ with open (path , 'r' ) as f :
15+ text = f .read ().decode ('utf-8' )
16+ text_json = json .loads (text )
17+ return text_json
18+
19+
20+ def save_into_excel (content_dict , excel_name ):
21+ wb = xlwt .Workbook ()
22+ ws = wb .add_sheet ("student" , cell_overwrite_ok = True )
23+ row = 0
24+ col = 0
25+
26+ for k , v in sorted (content_dict .items (),key = lambda d :d [0 ]):
27+ ws .write (row , col , k )
28+ for i in v :
29+ col += 1
30+ ws .write (row , col , i )
31+
32+ row += 1
33+ col = 0
34+
35+ wb .save (excel_name )
36+
37+
38+ if __name__ == "__main__" :
39+ read_content = read_txt (os .path .join (os .path .split (__file__ )[0 ], 'student.txt' ))
40+ save_into_excel (read_content , 'student.xls' )
41+
42+
Original file line number Diff line number Diff line change 1+ {
2+ "1":["张三",150,120,100],
3+ "2":["李四",90,99,95],
4+ "3":["王五",60,66,68]
5+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+ # coding=utf-8
3+
4+ """
5+ 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
6+ 请将上述内容写到 city.xls 文件中,如下图所示:
7+ """
8+
9+ import os
10+ import json
11+ import xlwt
12+
13+ def read_txt (path ):
14+ with open (path , 'r' ) as f :
15+ text = f .read ().decode ('utf-8' )
16+ text_json = json .loads (text )
17+ return text_json
18+
19+
20+ def save_into_excel (content_dict , excel_name ):
21+ wb = xlwt .Workbook ()
22+ ws = wb .add_sheet ("city" , cell_overwrite_ok = True )
23+ row = 0
24+ col = 0
25+
26+ for k , v in sorted (content_dict .items (),key = lambda d :d [0 ]):
27+ ws .write (row , col , k )
28+ col += 1
29+ ws .write (row , col , v )
30+
31+ row += 1
32+ col = 0
33+
34+ wb .save (excel_name )
35+
36+
37+ if __name__ == "__main__" :
38+ read_content = read_txt (os .path .join (os .path .split (__file__ )[0 ], 'city.txt' ))
39+ save_into_excel (read_content , 'city.xls' )
Original file line number Diff line number Diff line change 1+ {
2+ "1" : "上海",
3+ "2" : "北京",
4+ "3" : "成都"
5+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+ # coding=utf-8
3+
4+ """
5+ 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:
6+ 请将上述内容写到 numbers.xls 文件中,如下图所示:
7+ """
8+
9+ import os
10+ import json
11+ import xlwt
12+
13+ def read_txt (path ):
14+ with open (path , 'r' ) as f :
15+ text = f .read ().decode ('utf-8' )
16+ text_json = json .loads (text )
17+ return text_json
18+
19+
20+ def save_into_excel (content_dict , excel_name ):
21+ wb = xlwt .Workbook ()
22+ ws = wb .add_sheet ("numbers" , cell_overwrite_ok = True )
23+ row = 0
24+ col = 0
25+ for i in content_dict :
26+ for k in i :
27+ ws .write (row , col , k )
28+ col += 1
29+ row += 1
30+ col = 0
31+
32+ wb .save (excel_name )
33+
34+
35+ if __name__ == "__main__" :
36+ read_content = read_txt (os .path .join (os .path .split (__file__ )[0 ], 'numbers.txt' ))
37+ save_into_excel (read_content , 'numbers.xls' )
Original file line number Diff line number Diff line change 1+ [
2+ [1, 82, 65535],
3+ [20, 90, 13],
4+ [26, 809, 1024]
5+ ]
You can’t perform that action at this time.
0 commit comments