给定一个包含交换机名称的文件,端口、传入的BPS、传出的BPS,需要找出最上面的对话开关。
cat machine.csvSWITCH, PORTS, IBPS,OBPS
ARIS, ge-0/1, 5800000000, 5800000000
CISCO, ge-0/2, 1000000000, 5700027720
JUNIPER, ge-0/3, 2000000000,3000000000
HPE, ge-0/4,3000000000,4000000000在这种情况下,应该返回机器名ARIS。
如果您能帮忙的话,我曾经尝试过从文件中解析并创建一个字典,但是在这一点上我不知道如何继续下去。
def readFile(path, machine_dict):
with open('machine.csv', "r") as dataset:
_, *props = dataset.readline().split(',')
for line in dataset:
name, *vals = line.split(',')
machine_props = {}
for i in range(len(props)):
machine_props[props[i].strip('\n')] = vals[i].strip('\n')
machine_dict[name] = machine_props发布于 2022-01-14 21:53:50
首先,使用csv库读取文件:
import csv
with open("machine.csv", "r") as f:
reader = csv.reader(f)
# Skip header
next(reader)
# Convert to a list of tuples
data = list(reader)现在,data是
[['ARIS', ' ge-0/1', ' 5800000000', ' 5800000000'],
['CISCO', ' ge-0/2', ' 1000000000', ' 5700027720'],
['JUNIPER', ' ge-0/3', ' 2000000000', '3000000000'],
['HPE', ' ge-0/4', '3000000000', '4000000000']]然后,使用索引2和3的OBPS或IBPS的较大值对数据进行排序。
highest = sorted(data, key=lambda x: max(int(x[2]), int(x[3])))[-1]
print(highest[0])输出:
ARIShttps://stackoverflow.com/questions/70716338
复制相似问题