Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3977506
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:55:54+00:00 2026-05-20T04:55:54+00:00

Hi I have a code as given below def value(): file=open(‘C:/Documents and Settings/242481/My Documents/file.csv’,’r’)

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:55:55+00:00Added an answer on May 20, 2026 at 4:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found a code snippet as given below which is used to disable
In my code I have to match below 3 types of data abcd:xyz:def def:xyz
I have the HTML code given below. When I try to submit the inner
How do I pass arguments using ActionScript's event-listener? I have code, as given below,
In the example enumeration code given in this question , reproduced below, why does
Executing the below code gives me the following exception on the last line: InvalidOperationException:
I am trying to implement a function. My code is given below. I want
I have an HTML page with some <code> segments, and I have given them
I have some code that gives a user id to a utility that then
I have the following code, which will not work. The javascript gives no errors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.