将python字典写成Excel

问题描述 投票:0回答:2

我有一个非常长的python字典,其中包含一些键,其中包含更多的字典,如下面的示例所示

{
       "dscpTagValue": {"data": 10,
                        "accuracy": Low Drop"}
       "description": "Latency Insensitive"
}
,
{
       "dscpTagValue": {"data": 10,
                        "accuracy": Low Drop"}
       "description": "Latency Insensitive"
}
{
       "dscpTagValue": {"data": 10,
                        "accuracy": Low Drop"}
       "description": "Latency Insensitive"
}

我如何将其导出到excel?某些词典可能在其中包含多个词典,某些词典可能仅在键和值中没有其他词典。

python excel csv
2个回答
0
投票
data = [ { "dscpTagValue": {"data": 10, "accuracy": "Low Drop"}, "description": "Latency Insensitive" }, . . . ] import csv len_keys = [len(d.keys()) for d in data] # no. of keys of each dictionary csv_columns = list(data[np.argmax(len_keys)]) # csv column headers == longest dict (keys) try: with open('file.csv', 'w') as f: writer = csv.DictWriter(f, fieldnames=csv_columns) writer.writeheader() for d in data: writer.writerow(d) except IOError: print('IOError')

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.