RakeshKirola RakeshKirola - 3 days ago 7
Python Question

Python method is not returning all the rows of csv file

I have a csv file in below format:

TestCase,Flag

test1,Y

test2,Y

test3,Y

test4,Y

TestCase and Flag are the column names.

Now in order to read all the rows of above csv file,i have written below code:

import csv
import os

csv.register_dialect(
'usercredentials',
delimiter=',',
quotechar='"',
doublequote=True,
skipinitialspace=True,
lineterminator='\r\n',
quoting=csv.QUOTE_MINIMAL
)

def testexecution():


with open('location of above csv file', 'r+') as file:
data = csv.DictReader(file, dialect='usercredentials')
for row in data:
testcase = row['TestCase']
flag = row['Flag']
return testcase, flag


But this method returns only the last row of the csv file,whereas i want this method to return all the rows.

Please suggest.

Answer

In the for loop, you are replacing the value of the testcase and flag variables in each iteration and finally you are returning values from the last row. You probably want to use a dict or a list in which to append those values in each iteration and finally return those.

Comments