python每日一练:0015题
2020-12-13 06:01
标签:write com txt findall += inf with 北京 信息 第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示: 请将上述内容写到 city.xls 文件中,如下图所示: 代码如下: python每日一练:0015题 标签:write com txt findall += inf with 北京 信息 原文地址:https://www.cnblogs.com/xuxiaowen1990/p/11162013.html{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
import xlwt
import re
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet(‘city‘)
reg = re.compile(‘"(\d+)" : "(.*?)"‘)
count = 0
with open(‘city.txt‘,‘r‘,encoding=‘utf-8‘) as f:
content = f.read()
s = reg.findall(content)
for i in s:
worksheet.write(count, 0, label=i[0])
worksheet.write(count, 1, label=i[1])
count += 1
workbook.save(‘city.xls‘)
下一篇:python面试题