Skip to content

Commit f41a9dd

Browse files
committed
Add 0015 file
1 parent a459618 commit f41a9dd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Drake-Z/0015/0015.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'''第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
5+
{
6+
"1" : "上海",
7+
"2" : "北京",
8+
"3" : "成都"
9+
}
10+
请将上述内容写到 city.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, 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)
29+
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('0015/city.xls')
39+
40+
41+
42+
if __name__ == '__main__':
43+
file = open('0015/city.txt', 'r', encoding='utf-8')
44+
hangshu = 3
45+
lieshu = 1
46+
re1 = '"(.*?)" :'
47+
re2 = ': "(.*?)"'
48+
read_data(file.read(), re1, re2)

0 commit comments

Comments
 (0)