# coding:utf-8 import os import pandas as pd def save_execl(keys,data): # 处理可能包含list等类型的value values = [] for i in range(len(data)): values.append(str(data[i])) # print(values) # 写入数据 df = pd.DataFrame([values], columns=keys) # 保存到本地excel xlsx_name = "../data/sit/test.xlsx" df.to_excel(xlsx_name, index=False) if __name__ == "__main__": # 读取json文件内容 f = open("../experiment/data/xxx.json", encoding="utf-8") con = eval(f.read()) # 获取json keys及value keys = list(con.keys()) values = list(con.values()) # print(keys) # print(values) # 调用write_xlsx函数写入execl文件 save_execl(keys, values)
# coding:utf- import xlrd, xlwt # 单独引用xlutils 下的copy 写法为copy,如果仅引用xlutils 使用copy时为xlutils.copy.copy from xlutils.copy import copy # 写入execl def write_xlsx(keys, values): filesheet = "sheet" filepath = "../data/sit/aaa.xlsx" try: # 打开Execl wb = xlrd.open_workbook(filepath) # 找不到文件 except FileNotFoundError as ube: # 新建工作簿 wb = xlwt.Workbook('utf-8') # 创建工作表 ws = wb.add_sheet(filesheet) # 保存到新建工作簿 wb.save(filepath) # 打开文件 wb = xlrd.open_workbook(filepath) finally: # 想execl 写入文件 cp_wb = copy(wb) cp_ws = cp_wb.get_sheet(filesheet) # copy 直接修改单元格内容 # 循环写入keys到表格第一行 for i in range(len(keys)): cp_ws.write(0, i, keys[i]) # 循环写入value 到表格第二行 for i in range(len(values)): # values 可能为dict 所以需要转换为str cp_ws.write(1, i, str(values[i])) # 保存文件 cp_wb.save(filepath) if __name__ == "__main__": # 读取json文件内容 f = open("../experiment/data/xxx.json", encoding="utf-8") con = eval(f.read()) # 获取json keys及value keys = list(con.keys()) values = list(con.values()) # 调用write_xlsx函数写入execl文件 write_xlsx(keys, values)