Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中将内部字典写入.xlsx文件_Python_Excel - Fatal编程技术网

如何在Python中将内部字典写入.xlsx文件

如何在Python中将内部字典写入.xlsx文件,python,excel,Python,Excel,假设我有以下词典: { "sheet_A": { "2017": { "col1": "a", "col2": "b", "col3": "c", "col4": "d" }, "2018": { "col1": "aa", "col2": "bb", "col3

假设我有以下词典:

{
    "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列

即使在数据量相当大的情况下,与包结合使用的包也应该能够处理此问题

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

你现在有什么代码?它提供了什么输出(包括错误和警告消息)?@AgihammerTheep更新了我的问题,提供了更多的背景信息。