forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (41 loc) · 1.11 KB
/
main.py
File metadata and controls
56 lines (41 loc) · 1.11 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding:utf-8 -*-
'''
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}
请将上述内容写到 student.xls 文件中。
@Author monkey
@Date 2017-8-31
'''
import json
import xlwt
def getStudent():
with open('student.txt', 'r', encoding = 'UTF-8') as file:
text = ''
for line in file:
text = text + line
stu_json = json.loads(text, encoding = 'UTF-8')
print(stu_json)
writeInXLS(stu_json)
def writeInXLS(dict):
fileName = 'student.xls'
# 创建 xls 文件
file = xlwt.Workbook(encoding = 'utf-8')
# 创建 表
sheet = file.add_sheet('student', cell_overwrite_ok=True)
row = 0
col = 0
for k, v in sorted(dict.items(), key=lambda d:d[0]):
sheet.write(row, col, k)
for i in v:
col += 1
sheet.write(row, col, i)
row += 1
col = 0
file.save(fileName)
print('写入成功')
if __name__ == '__main__':
getStudent()