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 6645539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:17:59+00:00 2026-05-26T00:17:59+00:00

I already have written what I need for identifying and parsing the value I

  • 0

I already have written what I need for identifying and parsing the value I am seeking, I need help writing a column to the csv file (or a new csv file) with the parsed value. Here’s some pseudocode / somewhat realistic Python code for what I am trying to do:

# Given a CSV file, this function creates a new CSV file with all values parsed
def handleCSVfile(csvfile):
  with open(csvfile, 'rb') as file:
    reader = csv.reader(file, delimiter=',', lineterminator='\n')
    for row in reader:
        for field in row:
          if isWhatIWant(field):
            parsedValue = parse(field)
            # write new column to row containing parsed value

I’ve already written the isWhatIWant and parse functions. If I need to write a completely new csv file, then I am not sure how to have both open simultaneously and read and write from one into the other.

  • 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-26T00:18:00+00:00Added an answer on May 26, 2026 at 12:18 am

    I’d do it like this. I’m guessing that isWhatIWant() is something that is supposed to replace a field in-place.

    import csv
    
    def handleCSVfile(infilename, outfilename):
        with open(infilename, 'rb') as infile:
            with open(outfilename, 'wb') as outfile:
                reader = csv.reader(infile, lineterminator='\n')
                writer = csv.writer(outfile, lineterminator='\n')
    
                for row in reader:
                    for field_index, field in enumerate(row):
                        if isWhatIWant(field):
                            row[field_index] = parse(field)
                    writer.writerow(row)
    

    This sort of pattern occurs a lot and results in really long lines. It can sometimes be helpful to break out the logic from opening and files into a different function, like this:

    import csv
    
    def load_save_csvfile(infilename, outfilename):
        with open(infilename, 'rb') as infile:
            with open(outfilename, 'wb') as outfile:
                reader = csv.reader(infile, lineterminator='\n')
                writer = csv.writer(outfile, lineterminator='\n')
    
                read_write_csvfile(reader, writer)
    
    def read_write_csvfile(reader, writer)
        for row in reader:
            for field_index, field in enumerate(row):
                if isWhatIWant(field):
                    row[field_index] = parse(field)
            writer.writerow(row)
    

    This modularizes the code, making it easier for you to change the way the files and formats are handled from the logic independently from each other.

    Additional hints:

    • Don’t name variables file as that is a built-in function. Shadowing those names will bite you when you least expect it.
    • delimiter=',' is the default so you don’t need to specify it explicitly.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help converting Morse Code to English. I have already written and tested
I need a method written in Ruby which computes Variations. I have already written
In my new job, they have already written an httphandler to handle all the
I'm working on a video codec for OMAP3430. I already have code written in
I am having difficulties referencing all 3. I have already written my XML,XSD and
This is similar to this question, except the code is already written. I have
I have CSV file with sample data in this form : 220 30 255
Im working on a little hobby project. I already have written the code to
I have written a C++ class that I need to share an instance of
I'm fairly new to programming C# and I have written a program that uses

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.