第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:
{
"1" : "上海",
"2" : "北京",
"3" : "成都"
}
请将上述内容写到 city.xls 文件中,如下图所示:
代码如下:
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')