Skip to content

Commit 9a92fb7

Browse files
committed
Fix 0014
1 parent df9c418 commit 9a92fb7

2 files changed

Lines changed: 22 additions & 26 deletions

File tree

Drake-Z/0014/0014.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,24 @@
1111

1212
__author__ = 'Drake-Z'
1313

14-
import os
15-
import re
14+
import json
1615
from collections import OrderedDict
17-
import xlwt
16+
from openpyxl import Workbook
17+
from openpyxl.styles import Font
1818

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('student.xls')
19+
def txt_to_xlsx(filename):
20+
file = open(filename, 'r', encoding = 'UTF-8')
21+
file_cintent = json.load(file, encoding = 'UTF-8')
22+
print(file_cintent)
23+
workbook = Workbook()
24+
worksheet = workbook.create_sheet(title = 'student')
25+
font1 = Font(color = 'FF9632')
26+
font2 = Font(color = '0000FF')
27+
for i in range(1, len(file_cintent)+1):
28+
worksheet.cell(row = i, column = 1).value = i
29+
for m in range(0, len(file_cintent[str(i)])):
30+
worksheet.cell(row = i, column = m+2).value = file_cintent[str(i)][m]
31+
workbook.save(filename = 'student.xlsx')
4032

4133
if __name__ == '__main__':
42-
file = open('student.txt', 'r', encoding='utf-8')
43-
read_data(file.read())
34+
txt_to_xlsx('student.txt')

Drake-Z/0014/student.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"1":["张三",150,120,100],
3+
"2":["李四",90,99,95],
4+
"3":["王五",60,66,68]
5+
}

0 commit comments

Comments
 (0)