ingenium techfest ingenium techfest - 5 months ago 12
Python Question

For loop not working in python with csv file

I am writing a script in python to read a .csv file and print it on the basis of row.I am not getting any error while executing, but it doesn't get printed.
The code is as follows.

import os
import csv

os.chdir('C:\ingenium')

exampleFile = open('testt.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
for row in exampleReader:
print (row)
exampleFile.close()


Later I have found out that the word "row" doesnt have any significance in code as I replace "row" by any random string (for eg:"gcgc") it doesnt show any error.
.
I am using Pycharm as IDE.

Answer

Try this:

import os

import csv

os.chdir('C:\ingenium')

exampleFile = open('testt.csv','r')
exampleReader = csv.reader(exampleFile)
for row in exampleReader:
    print (row)
exampleFile.close()

As moses said, you already exhausted the csv.reader by importing the csv to list.