所以基本上这个代码的目标是报告正在使用一组设备上的哪些端口。
代码如下:
`$` 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
这就是问题所在,这一行应该为我提供了名为 'L1 switches/L1 Switch 10G PLS.BLLAB/Blade01'
AND 的端口数量根据.csv文件显示为 'Reserved'
。
这可能是什么想法?
谢谢!
答案 0 :(得分:1)
在第二个循环中,您应该使用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') `$`