0

Hi I have a code as given below

def value():
    file=open('C:/Documents and Settings/242481/My Documents/file.csv','r')
    for line in file:
        return "<TOTAL>"+line+"</TOTAL>"

when i execute the script only the first row of the csv file is returned. how do i get all the rows in the csv file in the for loop.

Thanks in advance : Aadith

Mark Longair
  • 446,582
  • 72
  • 411
  • 327

1 Answers1

0

That's because the return returns from the function with the first line on the first iteration through the loop.

You could extract the values from line on each iteration of the for loop using regular expressions, but it's a much better idea to just use the the csv module instead of writing your own ad-hoc CSV parser. That means that you don't have to worry about getting the rules about quoting right, for instance. By way of an example, supposing you want to get the total of all the numbers in the second column, you could do:

import csv

def total_of_second_column():
    total = 0.0
    fp = open('C:/Documents and Settings/242481/My Documents/file.csv')
    try:
        cr = csv.reader(fp)
        for row in cr:
            total += float(row[1])
        return total
    finally:
        fp.close()

... although of course you can do anything arbitrarily complex with the values you find in row[0], row[1], etc. on each iteration of the for loop.

Update: In the comment below you ask "is there any way i can execute the return statement as many times as the number of rows in csv file.?"

It sounds as if you might be looking for the yield keyword here. There's a great explanation of generators and yield in the question The Python yield keyword explained but to give a brief example of how you might use it in your example, you could do:

def two_column_products():
    fp open('C:/Documents and Settings/242481/My Documents/file.csv')
    try:
        cr = csv.reader(fp)
        for row in cr:
            yield float(row[1]) * float(row[2])
    finally:
        fp.close()

for result in two_column_products():
    print "got result:", result

This will print the product of the second and third column of each row in turn.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • What i want to do is i ave to pull of data from one row at a time and execute a code with the values paarameterized into the code. – user619348 Feb 23 '11 at 08:33
  • @user619348: I added an example showing how to use the CSV module to sum the values in the second column - however, you could do anything you like with row[0], row[1], etc. in the inner loop. – Mark Longair Feb 23 '11 at 08:43
  • I am writting this code in a soa testing s/w parasoft soatest. Once i run the test step the code included will pull the first data from a csv file put the value into the step and run somethin. This shd carry on for all the rows in the csv file. – user619348 Feb 23 '11 at 08:55
  • @user619348: yes, I think that example is what you want - you just have to replace `total += float(row[1])` with whatever you mean by "run somethin". I've tidied up my answer a bit, anyway. – Mark Longair Feb 23 '11 at 09:12
  • is there any way i can execute the return statement as many times as the number of rows in csv file.? – user619348 Feb 23 '11 at 10:15
  • @user619348: in a way, yes... I've updated my answer with an example of using `yield` to do something like that. – Mark Longair Feb 23 '11 at 10:30
  • but i don want a print statement. it has to return the values. – user619348 Feb 23 '11 at 12:40
  • @user619348: the yield **is** returning a value, I was just printing what's returned to show that. You can equally yield a tuple of values or the whole row... – Mark Longair Feb 23 '11 at 12:44
  • @MARK: how do i return a specific row in the excel file. if i want to return only the value at A3, how do i get it.? – user619348 Feb 24 '11 at 05:25
  • @user619348: you should ask that as a new question on the site - it's different enough that I don't want to just append more stuff to this answer. (Incidentally, you should consider accepting and/or upvoting my answer, if it's been useful.) – Mark Longair Feb 24 '11 at 06:46
  • @MARK: sorry i cant upvote ur answer as i don have the required reputation to do dat. – user619348 Feb 24 '11 at 06:55