编程语言
首页 > 编程语言> > python每日一练:0015题

python每日一练:0015题

作者:互联网

第 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')

 

标签:count,city,0015,python,worksheet,每日,workbook,txt
来源: https://www.cnblogs.com/xuxiaowen1990/p/11162013.html