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?

Cleb
  • 25,102
  • 20
  • 116
  • 151
Gonzalo
  • 1,084
  • 4
  • 20
  • 40
  • 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`. – Holloway Feb 22 '16 at 10:06
  • Hello, no, actually would be nice if they were in 6 records, one per row/line. – Gonzalo Feb 22 '16 at 10:20
  • actually, i am already using the writerows.. – Gonzalo Feb 22 '16 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. – Holloway Feb 22 '16 at 10:28
  • Possible duplicate of [CSV in Python adding an extra carriage return](http://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return) – Gall Feb 22 '16 at 11:27

2 Answers2

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']

dimid
  • 7,285
  • 1
  • 46
  • 85
  • still getting an error: line 6, in for row in f: ValueError: I/O operation on closed file. – Gonzalo Feb 22 '16 at 10:18
  • 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. – Gonzalo Feb 22 '16 at 10:19
  • `f` is the reader as in your code above `f = csv.reader(csvfile)`, right? – dimid Feb 22 '16 at 10:23
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.

Holloway
  • 6,412
  • 1
  • 26
  • 33
  • yeah, i got it :), trying now to attribute the varibale to row[1] – Gonzalo Feb 22 '16 at 10:39
  • 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 – Gonzalo Feb 22 '16 at 10:41
  • Print `row` before the `firstvalue =` line and see what's it's trying to access? Have you looked at the file that was written? – Holloway Feb 22 '16 at 10:44
  • data file looks fine: `36098,96,26324,9678,7,399` and if i print i have this `['36098', '96', '26324', '9678', '7', '399'] []` – Gonzalo Feb 22 '16 at 10:47
  • 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). – Holloway Feb 22 '16 at 10:52
  • i am using windows. So it seems that is that .. making problem since beginning, any idea what could be done? thanks a lot! – Gonzalo Feb 22 '16 at 10:55
  • Add something like `if not row: continue` at the top of the for loop? – Holloway Feb 22 '16 at 11:13