Skip to content

Commit a3ee5a2

Browse files
committed
Fix 0016
1 parent ea7642d commit a3ee5a2

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

Drake-Z/0016/0016.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,20 @@
1111

1212
__author__ = 'Drake-Z'
1313

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

19-
def shuju(data, re1, re2):
20-
c = OrderedDict([])
21-
re_xuhao = re.compile(r'%s' % re1)
22-
re_yuansu = re.compile(r'%s' % re2)
23-
a = re_xuhao.findall(data) #得到序号
24-
if len(a)==0:
25-
for d in range(0, hangshu):
26-
a.append(d)
27-
b = re_yuansu.findall(data) #得到具体数据
28-
for m, n in zip(a, b): #将数据转为Dict
29-
n = re.split(r',', n)
30-
c[m] = n
31-
writeFlie(c, hangshu, lieshu)
32-
33-
def writeFlie(dictdata, hangshu, lieshu):
34-
workbook = xlwt.Workbook(encoding = 'utf-8') #创建工作薄
35-
worksheet = workbook.add_sheet('My Worksheet') #创建表
36-
num = list(dictdata.keys()) #得到序号
37-
for i in range(0, hangshu):
38-
for m in range(0, lieshu):
39-
worksheet.write(i, m, label = dictdata[num[i]][m])
40-
workbook.save('numbers.xls')
18+
def txt_to_xlsx(filename):
19+
file = open(filename, 'r', encoding = 'UTF-8')
20+
file_cintent = json.load(file, encoding = 'UTF-8')
21+
print(file_cintent)
22+
workbook = Workbook()
23+
worksheet = workbook.create_sheet(title = 'numbers')
24+
for i in range(1, len(file_cintent)+1):
25+
for m in range(1, len(file_cintent[i-1])+1):
26+
worksheet.cell(row = i, column = m).value = file_cintent[i-1][m-1]
27+
workbook.save(filename = 'numbers.xls')
4128

4229
if __name__ == '__main__':
43-
file = open('numbers.txt', 'r', encoding='utf-8')
44-
hangshu = 3
45-
lieshu = 3
46-
re1 = '%'
47-
re2 = '\[(.*?)\]'
48-
shuju(file.read(), re1, re2)
30+
txt_to_xlsx('numbers.txt')

0 commit comments

Comments
 (0)