我是Python的新手,我正在寻找有关如何从以下嵌套字典中创建* xlsx文件的指导。我知道我必须使用像openpyxl这样的包来生成* xlsx文件,并且我必须使用一些嵌套的for循环或字典理解来获取数据。但是我无法将所有东西放在一起。任何帮助/提示都非常欢迎。
snapshot = {
"SHAMk98bspucm02.some-domain.invalid": [
{
"Name": "SEP1CDEA7837E70",
"Device Class": "Phone",
"Model": 683,
"Description": "+4912345556661 - BFE Alexandra Balzer",
"Directory Number": "+4912345556661",
"Registered On": "SHAMk98bspucm02.some-domain.invalid",
"IP Address": "192.29.53.112",
"Status": "Registered"
},
{
"Name": "SEP1CDEA7837E12",
"Device Class": "Phone",
"Model": 683,
"Description": "+4912345556662 - Michael H\u00f6nemann",
"Directory Number": "+4912345556662",
"Registered On": "SHAMk98bspucm02.some-domain.invalid",
"IP Address": "192.29.41.111",
"Status": "Registered"
}
],
"shamk98bspucm01.some-domain.invalid": [
{
"Name": "CSFOSTERAUC",
"Device Class": "Phone",
"Model": 503,
"Description": "+4912345556663 - Christian Osterauer",
"Directory Number": "+4912345556663",
"Registered On": "shamk98bspucm01.some-domain.invalid",
"IP Address": "192.28.11.13",
"Status": "Registered"
},
{
"Name": "CSFTHURNERF",
"Device Class": "Phone",
"Model": 503,
"Description": "+4912345556665 - Florian Thurner",
"Directory Number": "+4912345556665",
"Registered On": "shamk98bspucm01.some-domain.invalid",
"IP Address": "192.28.171.12",
"Status": "Registered"
}
],
"skolrzucm03.some-domain.invalid": [
{
"Name": "CSFWIESBOMI",
"Device Class": "Phone",
"Model": 503,
"Description": "+4912345556665 - Wiesboeck Michael",
"Directory Number": "+4912345556665",
"Registered On": "skolrzucm03.some-domain.invalid",
"IP Address": "192.28.11.81",
"Status": "Registered"
},
{
"Name": "SEP6C6CD3A5C94E",
"Device Class": "Phone",
"Model": 684,
"Description": "+4912345556666 - Nina Schraud",
"Directory Number": "+4912345556666",
"Registered On": "skolrzucm03.some-domain.invalid",
"IP Address": "192.28.11.84",
"Status": "Registered"
}
]
}
* xlsx文件应如下所示:
|Name|Device Class|Model|Description|Directory Number|Registered On| IP Address| Status|
|SEP1CDEA7837E70|Phone|683|+4912345556661 - BFE Alexandra Balzer|+4912345556661|SHAMk98bspucm02.some-domain.invalid|192.29.53.112|Registered|
我尝试了以下代码来获取内部字典的值。
for cm_nodes,device_info in snapshot.items():
# print (cm_nodes,device_info) # Gets me keys and the values of the Outer Dict. Value is of type list
for device_info_list in device_info:
# print (device_info_list)
for device_key,device_value in device_info_list.items(): # Gets me all the Dict Values of the inner Dictionaries
print(device_value)
但是现在我被困住了。我有的问题: -如何确定“设备”的启动位置(“名称键”)和停止的位置(“状态”)
答案 0 :(得分:0)
对于.xlsx代,我将使用XlsxWriter和Pandas数据框。
我假设您在名为dictionary
的变量中拥有此数据。
import pandas as pd
# Get the "outer" dictionary keys
outer_dict_keys = dictinary.keys()
# Define the Pandas dataframe
df = pd.DataFrame(columns=dictinary["SHAMk98bspucm02.some-domain.invalid"] [0].keys()) # It is quite ugly :D, just lack of time ... change it!
# Iterate through "outer" dictionary
for outer_dict_key in outer_dict_keys:
# With this you get: SHAMk98bspucm02.some-domain.invalid, ...
interior_dict = dictinary[outer_dict_key][0] # The [0] is there because it is in a list
# Get the "interior" dictionary keys
interior_dict_keys = interior_dict.keys()
# This variable contains the data for one row.
row = dict()
for interior_dict_key in interior_dict_keys:
# With this you get: Name, Device Class, Model ..
row[interior_dict_key] = interior_dict[interior_dict_key]
# You can reach the values like this:
df = df.append(row, ignore_index=True)
# Create an xlsxwriter object and save the created Excel file
writer = pd.ExcelWriter('filename.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='sheet_name', index=False)
writer.save()
writer.close()
我希望它会对您有所帮助:)