尝试使用2列

2024-10-01 07:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,这段代码的基本目标是报告一组设备上的哪些端口正在被利用

代码如下:

`$` file_name = input('Enter File Name: ') + str('.csv')
total_ports = 500

my_reader = csv.reader(open(file_name))
ctr = 0
for total_reserved in my_reader:
    if total_reserved[2] == 'Reserved':
        ctr += 1
print(ctr, 'out of ', total_ports, 'PLS ports are reserved')

my_reader = csv.reader(open(file_name))
ctr_pls0 = 0
for pls0 in my_reader:
    if pls0[0] == 'L1 switches/L1 Switch 10G PLS0.BLLAB/Blade01' and total_reserved[2]  == 'Reserved':
      ctr_pls0 += 1

print(ctr_pls0, 'of these', ctr, 'are pls0 ports') `$`

这给了我以下输出

Enter File Name: 31.05.2018
175 out of  500 PLS ports are reserved
0 of these 175 are pls0 ports

Process finished with exit code 0

0 of these 175 are pls0 ports这就是问题所在,此行应该为我提供根据.csv文件显示为'L1 switches/L1 Switch 10G PLS.BLLAB/Blade01'的端口数量

你知道这是什么吗

谢谢


Tags: ofcsvnamel1myportsarereader
1条回答
网友
1楼 · 发布于 2024-10-01 07:17:32

在第二个循环中,应该使用pls0[2],而不是total_reserved[2]total_reserved包含第一个循环中文件的最后一行

但根本没有理由有两个循环,只需在第一个循环中增加两个变量

with csv.reader(open(file_name)) as my_reader:
    ctr = 0
    ctr_pls0 = 0
    for total_reserved in my_reader:
        if total_reserved[2] == 'Reserved':
            ctr += 1
            if total_reserved[0] == 'L1 switches/L1 Switch 10G PLS0.BLLAB/Blade01'
                ctr_pls0 += 1
print(ctr, 'out of ', total_ports, 'PLS ports are reserved')
print(ctr_pls0, 'of these', ctr, 'are pls0 ports') `$`

相关问题 更多 >