Reputation: 1
I'm reading a two column .csv file of variable length. I have some code written that should be able to read the data bar the first line into an x Data column and a y Data column. Here is the code:
def csvReader(filename):
with open(filename) as csvFile:
csvReader = csv.reader(csvFile, delimiter = ',')
rowCount = sum(1 for row in csvReader)
xData = np.zeros(rowCount)
yData = np.zeros(rowCount)
line_count = 0
firstLine = True
for row in csvReader:
print(row)
if firstLine:
firstLine = False
continue
xData[line_count] = row[0]
yData[line_count] = row[1]
line_count += 1
return xData,yData
It outputs an array of zeros, and the console never shows any printed output, which seems to imply that the entire for loop is getting skipped. Any help on this issue would be appreciated.
Upvotes: 0
Views: 37
Reputation: 780724
You're exhausting the iterator when you do
rowCount = sum(1 for row in csvReader)
You need to rewind the file to read it again.
csvFile.seek(0)
Upvotes: 3