Python:在字典中排列和排序来自 Excel Pandas 的数据
Python: Arrange and order data in a dictionary from Excel Pandas
我可以使用 Pandas.
从 Excel 文件中读取特定列
Excel 文件:
代码:
import os
import pandas as pd
excel_file = "File.xlsx"
data = pd.read_excel(os.path.join("./", excel_file), usecols="A:B,H:I")
df = pd.DataFrame(data)
dict_data = df.to_dict(orient="dict")
dict_data
看起来像这样:
{
"VLAN ID": {0: 100, 1: 200, 2: 300},
"VLAN Name": {0: "MGMT", 1: "Users", 2: "Phones"},
"Gateway": {0: "10.0.0.1", 1: "172.16.0.1", 2: "192.168.1.1"},
"Subnet Mask": {0: "255.0.0.0", 1: "255.255.0.0", 2: "255.255.255.0"}
}
我想将 dict_data
转换为如下所示:
{
"vlans": {
{
"id": 100,
"name": "MGMT",
"ipaddr": "10.0.0.1",
"mask": "255.0.0.0",
},
{
"id": 200,
"name": "Users",
"ipaddr": "172.16.0.1",
"mask": "255.255.0.0",
},
{
"id": 300,
"name": "Phones",
"ipaddr": "192.168.1.1",
"mask": "255.255.255.0",
},
}
}
然后,将vlans
传给一个Jinja2模板来创建?我怎样才能实现这个输出?
Jinja2 模板
{% for vlan in vlans.items() %}
vlan {{ vlan["id"] }}
name {{ vlan["name"] }}
exit
!
interface vlan {{ vlan["id"] }}
ip address {{ vlan["ipaddr"] }} {{ vlan["mask"] }}
description {{ vlan["name"] }}
exit
!
{% endfor %}
来自 Jinja 的输出
vlan 100
name MGMT
exit
!
interface vlan 100
ip address 10.0.0.1 255.0.0.0
description MGMT
exit
!
vlan 200
name Users
exit
!
interface vlan 200
ip address 172.16.0.1 255.255.0.0
description Users
exit
!
vlan 300
name Phones
exit
!
interface vlan 300
ip address 192.168.1.1 255.255.255.0
description Phones
exit
!
似乎从 DataFrame 中这样做比 data_dict
:
更容易
rename
the columns to be the new values, then call to_dict
和 orient='records'
.
out = {
'vlans': df.rename(
columns={'VLAN ID': 'id',
'VLAN Name': 'name',
'Gateway': 'ipaddr',
'Subnet Mask': 'mask'}
).to_dict(orient='records')
}
out
:
{'vlans': [{'id': 100,
'ipaddr': '10.0.0.1',
'mask': '255.0.0.0',
'name': 'MGMT'},
{'id': 200,
'ipaddr': '172.16.0.1',
'mask': '255.255.0.0',
'name': 'Users'},
{'id': 300,
'ipaddr': '192.168.1.1',
'mask': '255.255.255.0',
'name': 'Phones'}]}
我可以使用 Pandas.
从 Excel 文件中读取特定列Excel 文件:
代码:
import os
import pandas as pd
excel_file = "File.xlsx"
data = pd.read_excel(os.path.join("./", excel_file), usecols="A:B,H:I")
df = pd.DataFrame(data)
dict_data = df.to_dict(orient="dict")
dict_data
看起来像这样:
{
"VLAN ID": {0: 100, 1: 200, 2: 300},
"VLAN Name": {0: "MGMT", 1: "Users", 2: "Phones"},
"Gateway": {0: "10.0.0.1", 1: "172.16.0.1", 2: "192.168.1.1"},
"Subnet Mask": {0: "255.0.0.0", 1: "255.255.0.0", 2: "255.255.255.0"}
}
我想将 dict_data
转换为如下所示:
{
"vlans": {
{
"id": 100,
"name": "MGMT",
"ipaddr": "10.0.0.1",
"mask": "255.0.0.0",
},
{
"id": 200,
"name": "Users",
"ipaddr": "172.16.0.1",
"mask": "255.255.0.0",
},
{
"id": 300,
"name": "Phones",
"ipaddr": "192.168.1.1",
"mask": "255.255.255.0",
},
}
}
然后,将vlans
传给一个Jinja2模板来创建?我怎样才能实现这个输出?
Jinja2 模板
{% for vlan in vlans.items() %}
vlan {{ vlan["id"] }}
name {{ vlan["name"] }}
exit
!
interface vlan {{ vlan["id"] }}
ip address {{ vlan["ipaddr"] }} {{ vlan["mask"] }}
description {{ vlan["name"] }}
exit
!
{% endfor %}
来自 Jinja 的输出
vlan 100
name MGMT
exit
!
interface vlan 100
ip address 10.0.0.1 255.0.0.0
description MGMT
exit
!
vlan 200
name Users
exit
!
interface vlan 200
ip address 172.16.0.1 255.255.0.0
description Users
exit
!
vlan 300
name Phones
exit
!
interface vlan 300
ip address 192.168.1.1 255.255.255.0
description Phones
exit
!
似乎从 DataFrame 中这样做比 data_dict
:
rename
the columns to be the new values, then call to_dict
和 orient='records'
.
out = {
'vlans': df.rename(
columns={'VLAN ID': 'id',
'VLAN Name': 'name',
'Gateway': 'ipaddr',
'Subnet Mask': 'mask'}
).to_dict(orient='records')
}
out
:
{'vlans': [{'id': 100,
'ipaddr': '10.0.0.1',
'mask': '255.0.0.0',
'name': 'MGMT'},
{'id': 200,
'ipaddr': '172.16.0.1',
'mask': '255.255.0.0',
'name': 'Users'},
{'id': 300,
'ipaddr': '192.168.1.1',
'mask': '255.255.255.0',
'name': 'Phones'}]}