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-
importos
15-
importre
16-
fromcollectionsimportOrderedDict
17-
importxlwt
14+
importjson
15+
fromopenpyxlimportWorkbook
16+
fromopenpyxl.stylesimportFont
1817

19-
defshuju(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-
iflen(a)==0:
25-
fordinrange(0, hangshu):
26-
a.append(d)
27-
b=re_yuansu.findall(data) #得到具体数据
28-
form, ninzip(a, b): #将数据转为Dict
29-
n=re.split(r',', n)
30-
c[m] =n
31-
writeFlie(c, hangshu, lieshu)
32-
33-
defwriteFlie(dictdata, hangshu, lieshu):
34-
workbook=xlwt.Workbook(encoding='utf-8') #创建工作薄
35-
worksheet=workbook.add_sheet('My Worksheet') #创建表
36-
num=list(dictdata.keys()) #得到序号
37-
foriinrange(0, hangshu):
38-
forminrange(0, lieshu):
39-
worksheet.write(i, m, label=dictdata[num[i]][m])
40-
workbook.save('numbers.xls')
18+
deftxt_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+
foriinrange(1, len(file_cintent)+1):
25+
forminrange(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)