I’m using the csv dictreader to read in a CSV file and for some reason unknown to me, it’s unable to locate the first column by name.
First few lines of dataset
ComputerName,Installation,HostPurpose,Status,Location,MIListener,Attribute,Priority,SslEnabled,ID
wloa2ama,WindowsHost,Controller,Operational,Default,None,,5,FALSE,1131
wloa2ama,WindowsHost,DataProcessor,Operational,Default,None,,5,FALSE,1131
wloa2ama,WindowsHost,,Operational,Default,None,,5,FALSE,1131
wloa29ma,WindowsHost,LoadGenerator,Operational,Default,None,,5,FALSE,1143
Expecting to see the value of the ComputerName
import csv
csv_file = open('LREHosts.csv', mode='r')
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
print (row["ComputerName"])
if line_count == 0:
print(f'{" ".join(row)}')
line_count += 1
print(f't{row["ComputerName"]} {row["Attribute"]} {row["Status"]}')
line_count += 1
print(f'Processed {line_count} lines.')
Getting an error Index not found when printing ComputerName
Traceback (most recent call last):
File “C:Userspk48521gitperf-api-testingperf-api-linux.py”, line 412, in
ReadHosts()
File “C:Userspk48521gitperf-api-testingperf-api-linux.py”, line 33, in ReadHosts
print (row[“ComputerName”])
~~~^^^^^^^^^^^^^^^^
KeyError: ‘ComputerName’