0

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?

5
  • Are all your values meant to be a single record in the csv? I think csv.writer has a writerow method that writes the values as a single entry that might be better. row[1] would then return okstatusN. Commented Feb 22, 2016 at 10:06
  • Hello, no, actually would be nice if they were in 6 records, one per row/line. Commented Feb 22, 2016 at 10:20
  • actually, i am already using the writerows.. Commented Feb 22, 2016 at 10:26
  • writerow vs writerows. One takes a list of values and puts them onto one line in the file, the other takes a list of lists and runs writerow on each list in turn. Your values are each in their own list so get added as new lines. Commented Feb 22, 2016 at 10:28
  • Possible duplicate of CSV in Python adding an extra carriage return Commented Feb 22, 2016 at 11:27

2 Answers 2

2

Your file contains empty lines, so you need to check the length of the row:

values = []
for row in f: 
    if len(row) > 0:
        values.append(row[0])

values is now ['36111', '96', '25887', '10128', '7', '398']

Sign up to request clarification or add additional context in comments.

3 Comments

still getting an error: line 6, in <module> for row in f: ValueError: I/O operation on closed file.
maybe will try to use diferent method to write the csv. and insert correctly the data per row, without empty rows in middle. what you think? thanks.
f is the reader as in your code above f = csv.reader(csvfile), right?
0

At the moment you're writing 6 rows into a csv file, with each row containing one column. To make a single row with six columns, you need to use a list of values, not each value in its own list.

ie change

data = [[agents125N], [okstatusN], [warningstatusN], [criticalstatusN], [agentdisabledN], [agentslegacyN]]

to

data = [[agents125N, okstatusN, warningstatusN, criticalstatusN, agentdisabledN, agentslegacyN]]

(a list containing one list of six values). Writing this with csv.writerows will result in

36111, 96, 25887, 10128, 7, 398

row[1] in your reading loop will return 96.

7 Comments

yeah, i got it :), trying now to attribute the varibale to row[1]
I dont understand, even changing the data format o csv, when I run the code import csv with open('agentstatus.csv', 'r') as csvfile: f = csv.reader(csvfile) for row in f: firstvalue = row[0] I am still getting ` firstvalue = row[0] IndexError: list index out of range` error
Print row before the firstvalue = line and see what's it's trying to access? Have you looked at the file that was written?
data file looks fine: 36098,96,26324,9678,7,399 and if i print i have this ['36098', '96', '26324', '9678', '7', '399'] []
It looks like it's adding an empty line at the end which is causing problems. Are you using windows or linux (not sure if that'd cause a difference but new lines are different).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.