# coding=utf-8 import xlwt import json """" 需求:将city文件存在Excel表格中 { "1" : "上海", "2" : "北京", "3" : "成都" } """ def load_to_json(filename): f = file(filename) json_data = json.load(f) return json_data def save_city_data_to_excel(data): data_excel = xlwt.Workbook() # 新建一个Excel文件 data_sheet = data_excel.add_sheet("sheet1") # 新建一个Excel表格 row = 0 for k, v in data.items(): data_sheet.write(row, 0, k) data_sheet.write(row, 1, v) row = row+1 data_excel.save("city4.xls") if __name__ == "__main__": save_city_data_to_excel(load_to_json("city.txt"))
运行结果:
请发表评论