假设我有以下字典:
{
"sheet_A": {
"2017": {
"col1": "a",
"col2": "b",
"col3": "c",
"col4": "d"
},
"2018": {
"col1": "aa",
"col2": "bb",
"col3": "cc",
"col4": "dd"
},
"2019": {
"col1": "aaa",
"col2": "bbb",
"col3": "ccc",
"col4": "ddd"
}
},
"sheet_B": {
"2017": {
"col1": "e",
"col2": "f",
"col3": "g",
"col4": "h"
},
"2018": {
"col1": "ee",
"col2": "ff",
"col3": "gg",
"col4": "hh"
},
"2019": {
"col1": "eee",
"col2": "fff",
"col3": "ggg",
"col4": "hhh"
}
}
}
我想以以下形式创建一个Excel文件(.xlsx
)(请注意两个不同的工作表):
我尝试使用xlwt
,但是它不适合我的用例,因为我每张纸要填充256列以上。
答案 0 :(得分:1)
即使在数据量非常大的情况下,pandas
软件包与xlsxwriter
软件包的组合也应该能够处理此问题。
# It is assumed that the data dictionary has been assigned the name `data_dict`
import pandas as pd
# Convert dictionaries to Pandas dataframes
df_A = pd.DataFrame(data_dict["sheet_A"]).transpose()
df_B = pd.DataFrame(data_dict["sheet_B"]).transpose()
# Create a Pandas Excel writer using XlsxWriter as the engine
with pd.ExcelWriter('workbook.xlsx', engine='xlsxwriter') as writer:
# Write each dataframe to a different worksheet
df_A.to_excel(writer, sheet_name='Sheet_A')
df_B.to_excel(writer, sheet_name='Sheet_B')