我有一个列表port_ids,它们的顺序正确。基于此,我从文件中获取值并将其存储在字典中。但是字典中存储的值与原始列表不对齐。这是我的代码:
import os
os.chdir('/var/lib/docker/volumes/kolla_logs/_data/openvswitch/')
port_ids=['qvoee855b93-ba', 'qvo9aa3a7d8-64', 'qvo2fc6e482-aa', 'qvo6a27cf40-8f']
def port_numb(text):
try:
with open('ovs-vswitchd.log') as f:
for line in f:
if line.find(text) != -1:
return line[97:100]
except Exception as ex:
print('Failed to open file {}'.format(ex))
ovs_port_numb = list(map(port_numb, port_ids))
d = dict(zip(port_ids, ovs_port_numb))
print d
输出: {'qvoee855b93-ba':'174','qvo2fc6e482-aa':'176','qvo6a27cf40-8f':'177','qvo9aa3a7d8-64':'175'}
我希望字典中的键与列表(port_ids)中的顺序相同。