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

  • SEARCH
  • Home
  • 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 7678399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:38:17+00:00 2026-05-31T17:38:17+00:00

I am complete newbie for programming and this is my first real program I

  • 0

I am complete newbie for programming and this is my first real program I am trying to write.

So I have this huge CSV file (hundreds of cols and thousands of rows) where I am trying to extract only few columns based on value in the field. It works fine and I get nice output, but the problem arises when I am try to encapsulate the same logic in a function.
it returns only first extracted row however print works fine.

I have been playing for this for hours and read other examples here and now my mind is mush.

import csv
import sys

newlogfile = csv.reader(open(sys.argv[1], 'rb'))
outLog = csv.writer(open('extracted.csv', 'w'))

def rowExtractor(logfile):
    for row in logfile:
        if row[32] == 'No':
            a = []
            a.append(row[44])
            a.append(row[58])
            a.append(row[83])
            a.append(row[32])
            return a

outLog.writerow(rowExtractor(newlogfile))
  • 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-31T17:38:18+00:00Added an answer on May 31, 2026 at 5:38 pm

    You are exiting prematurely. When you put return a inside the for loop, return gets called on the first iteration. Which means that only the firs iteration runs.

    A simple way to do this would be to do:

    def rowExtractor(logfile):
        #output holds all of the rows
        ouput = []
        for row in logfile:
            if row[32] == 'No':
                a = []
                a.append(row[44])
                a.append(row[58])
                a.append(row[83])
                a.append(row[32])
                output.append(a)
        #notice that the return statement is outside of the for-loop
        return output
    outLog.writerows(rowExtractor(newlogfile))
    

    You could also consider using yield

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

Sidebar

Related Questions

I'm a complete newbie at Haskell. I'm trying to compile this Haskell file I've
I'm a complete newbie to Windows and COM programming, trying to use com4j in
I'm a complete newbie in OCaml and trying to create a simple console program.
I'm really new at network-programming, so I hope this isn't a complete Newbie-question. I
I am a complete Android programming newbie. I have completed some tutorial examples like
Complete C# newbie here, so cut me some slack. I have this application that
I am a complete newbie to iOS development but not to programming. I am
I am complete newbie in mod_rewrite , and I have been going through some
I am a complete newbie to SSIS. I have a c#/sql server background. I
Complete newbie to programming :) i am fiddling around with a WordPress theme and

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.