2

Python newbie. I have csv data the looks like the following:

Record  Name    Cur     e12mo   e24mo   e48mo   state
3928    Joes    2000    200     400     0       CA,GA
1       Toms    19       1      2       0       AR,KS
1747    Mine    60       5      10      0       AR,CT
5023    Yours   5        12     24      0       FL
7041    Theirs  10       2      4       0       FL

Am entering code from tutorial as follows:

import numpy as np
import csv as csv

readdata = csv.reader(open('c:\MyData\BYLCsv.csv'))
for row in readdata:
    print(row)
data = []  
for row in readdata:
    data.append(row)
for row in data:
    print(row)
Header = data[0]
data.pop(0) 

Code bombs on the "Header = data[0]" statement. Everything runs up to there.

2
  • 1
    Code bombs And your PC explodes? OR what happens? Commented Mar 8, 2015 at 21:01
  • 1
    Please post the name of the error and the full traceback helpfully provided to you by Python. Commented Mar 8, 2015 at 21:02

2 Answers 2

2

In your first loop, you read the whole file, the file pointer is not reset afterwards:

import numpy as np
import csv as csv
with open('c:\MyData\BYLCsv.csv') as data:
    readdata = csv.reader(data)
    header = next(readdata)
    data = list(readdata)
print(header)
for row in data:
    print(row)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works well. Had to read up on 'with' . I am assuming the the statement 'data = list(readdata)" takes the data from readdata and creates the list. I can read any row now with something like "print(data[230])" .
1

Try to put everything in the same for:

import numpy as np
import csv as csv

readdata = csv.reader(open('c:\MyData\BYLCsv.csv'))
data = []  

for row in readdata:
    print(row)
    data.append(row)

for row in data:
    print(row)

Header = data[0]
data.pop(0) 

You are trying to iterate twice over the readdata iterator, and it can be consumed just once

3 Comments

Yuri is correct. The reasoning is that csv.reader opened the file for reading, and since you went through and printed everything, you are now at the end of the file. There is probably a way to seek back to the beginning of the file, or you can do as Yuri suggested and do your debug printing and appending in the same loop.
Don't know how to put everything in the same, very new to this. I used Daniels example and it worked great. Would like to see an example of what you mean. Is Daniels example the best way to do this type of thing( the most Pythonic)?
Yes, he did well. But what I tried to show to you is that your code failed because you did two fors like that: "for row in readdata", and just the first one ran ok, because readdata uses an iterator, and you can consume an iterator just once. Thus, after the first for, the data in readdata can't be used. So, if you use just one "for row in readdata" you'll be ok.

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.