2

This code only iterates through the number of rows once I would like to iterate through all the rows for the number of columns in the data I'm confused as to why it isn't iterating through the rows 7 times.

import  csv
from    mpl_toolkits.mplot3d import axes3d
import  matplotlib.pyplot   as  plt
import  numpy   as  np

sensorData      =   []
longitudinal    =   []
transverse      =   []

n=0
with open('test3.csv') as csvfile:
     readCsv = csv.reader(csvfile, delimiter =',')
     for x in range(0,7):    #problem HERE
         for row in readCsv:
             n+=1
             sensorData.append(float(row[x]))

             longitudinal.append(n)

             transverse.append(x)
4
  • what data do you expect to be in the csv file ? Does the row have 7 items ? Commented Jul 12, 2016 at 19:32
  • Yes the csv contains a specific number of columns, exactly 7 Commented Jul 12, 2016 at 19:33
  • Your for loops are in the wrong order. You are trying to read the entire CSV file 7 times, instead of looping over the columns of each row Commented Jul 12, 2016 at 19:36
  • I would like to order them specifically, so that they can be plotted on a 3d plot. I was thinking the easiest way to do that would be in the format [longitudinal,transverse,data]. There are 7 analog channels I am reading from over the same longitudinal (x) distance the transverse location (y) and each has a different analog measurement. Commented Jul 12, 2016 at 19:38

3 Answers 3

1

Similar question here: Reading from CSVs in Python repeatedly?

After you loop through the file, you need to reset the read position of csvfile.

    csvfile.seek(0)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! After adding this line at the end of the outer for loop it worked as hoped.
I would 100% upvote this however, I don't have the necessary reputation (15) to upvote...
1

Your code has a outer loop that will loop 7 times, and the inner loop will loop over each row. You need to swap the inner and outer loops.

For each row loop over each column

with open('test3.csv') as csvfile:
     readCsv = csv.reader(csvfile, delimiter =',')
     for row in readCsv:
         for x in range(0,7):
             n+=1
             sensorData.append(float(row[x]))

1 Comment

Some explanation of why the original code is wrong would be nice (yes it's obvious to us, but they wouldn't have asked if it was obvious to them)
0

Once you read all the rows in the file, the file data is exhausted and there's nothing left to read, so your for row in readCsv: loop immediately terminates.

If you want to reset the file and read it all over again, you'll need to close the file and open it again.

Comments

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.