I created a program to write a simple .csv (code below):
opencsv = open('agentstatus.csv', 'w')
a = csv.writer(opencsv)
data = [[agents125N],
[okstatusN],
[warningstatusN],
[criticalstatusN],
[agentdisabledN],
[agentslegacyN]]
a.writerows(data)
opencsv.close()
The .csv looks like this (it's with empty rows in the middle, but it's not a problem):
36111
96
25887
10128
7
398
Now I am trying to read the .csv and store each of this numbers in a variable, but without success, see below an example for the number 36111:
import csv
with open('agentstatus.csv', 'r') as csvfile:
f = csv.reader(csvfile)
for row in f:
firstvalue = row[0]
However, I get the error:
line 6, in <module>
firstvalue = row[0]
IndexError: list index out of range
Could you support me here?
csv.writerhas awriterowmethod that writes the values as a single entry that might be better.row[1]would then returnokstatusN.writerowvswriterows. One takes a list of values and puts them onto one line in the file, the other takes a list of lists and runswriterowon each list in turn. Your values are each in their own list so get added as new lines.