Skip to content

Commit a459618

Browse files
committed
Add 0014 file
1 parent 5656330 commit a459618

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Drake-Z/0014/0014.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:
5+
{
6+
"1":["张三",150,120,100],
7+
"2":["李四",90,99,95],
8+
"3":["王五",60,66,68]
9+
}
10+
请将上述内容写到 student.xls 文件中。'''
11+
12+
__author__ = 'Drake-Z'
13+
14+
import os
15+
import re
16+
from collections import OrderedDict
17+
import xlwt
18+
19+
def read_data(data):
20+
c = OrderedDict([])
21+
re_xuhao = re.compile(r'"([\d]+)":')
22+
re_yuansu = re.compile(r'\[(.*?)\]')
23+
a = re_xuhao.findall(data) #得到序号
24+
b = re_yuansu.findall(data) #得到具体数据
25+
for m, n in zip(a, b): #将数据转为Dict
26+
n = re.split(r',', n)
27+
n[0] = n[0][1:-1] #去除引号
28+
c[m] = n
29+
writeFlie(c)
30+
31+
def writeFlie(dictdata):
32+
workbook = xlwt.Workbook(encoding = 'utf-8') #创建工作薄
33+
worksheet = workbook.add_sheet('My Worksheet') #创建表
34+
num = list(dictdata.keys()) #得到序号
35+
for i in range(0, 3):
36+
worksheet.write(i, 0, label = num[i])
37+
for m in range(0, 4):
38+
worksheet.write(i, m+1, label = dictdata[num[i]][m])
39+
workbook.save('0014/student.xls')
40+
41+
if __name__ == '__main__':
42+
file = open('0014/student.txt', 'r', encoding='utf-8')
43+
read_data(file.read())

0 commit comments

Comments
 (0)