Skip to content

Commit ea7642d

Browse files
committed
Fix 0015
1 parent fd752db commit ea7642d

1 file changed

Lines changed: 14 additions & 29 deletions

File tree

Drake-Z/0015/0015.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,21 @@
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, 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-
b = re_yuansu.findall(data) #得到具体数据
25-
for m, n in zip(a, b): #将数据转为Dict
26-
n = re.split(r',', n)
27-
c[m] = n
28-
writeFlie(c, hangshu, lieshu)
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 = 'city')
25+
for i in range(1, len(file_cintent)+1):
26+
worksheet.cell(row = i, column = 1).value = i
27+
worksheet.cell(row = i, column = 2).value = file_cintent[str(i)]
28+
workbook.save(filename = 'city.xls')
2929

30-
def writeFlie(dictdata, hangshu, lieshu):
31-
workbook = xlwt.Workbook(encoding = 'utf-8') #创建工作薄
32-
worksheet = workbook.add_sheet('My Worksheet') #创建表
33-
num = list(dictdata.keys()) #得到序号
34-
for i in range(0, hangshu):
35-
worksheet.write(i, 0, label = num[i])
36-
for m in range(0, lieshu):
37-
worksheet.write(i, m+1, label = dictdata[num[i]][m])
38-
workbook.save('city.xls')
39-
4030
if __name__ == '__main__':
41-
file = open('city.txt', 'r', encoding='utf-8')
42-
hangshu = 3
43-
lieshu = 1
44-
re1 = '"(.*?)" :'
45-
re2 = ': "(.*?)"'
46-
read_data(file.read(), re1, re2)
31+
txt_to_xlsx('city.txt')

0 commit comments

Comments
 (0)