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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:25:22+00:00 2026-06-05T22:25:22+00:00

I need to write the output of the code I have to a file

  • 0

I need to write the output of the code I have to a file so I can call it later. I need to call the output not the original test1 file. the code I have that makes the output is below and works fine, I just can’t get it to a file a can call later.

 import csv 

 file1  = open('C:/Users/Gallatin/Documents/adamenergy.csv',"r") #Open CSV File in Read Mode 
 reader = csv.reader(file1)      #Create reader object which iterates over lines 

 class Object:                   #Object to store unique data 
  def __init__(self, name, produce, amount): 
    self.name = name 
    self.produce = produce 
    self.amount = amount 

 rownum = 0 #Row Number currently iterating over 
 list = []  #List to store objects 

 def checkList(name, produce, amount): 


    for object in list:  #Iterate through list         
    if object.name == name and object.produce == produce:  #Check if name and produce combination exists 
        object.amount += int(amount) #If it does add to amount variable and break out 
        return 

newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount 
list.append(newObject)  #Add to list and break out 


  for row in reader:  #Iterate through all the rows 
   if rownum == 0:  #Store header row seperately to not get confused 
    header = row 
  else: 
    name = row[0]  #Store name 
    produce = row[1]  #Store produce 
    amount = row[2]  #Store amount 

    if len(list) == 0:  #Default case if list = 0 
        newObject = Object(name, produce, int(amount)) 
        list.append(newObject) 
    else:  #If not... 
        checkList(name, produce, amount) 


rownum += 1 

 for each in list:
   print each.name,each.produce,each.amount

With the print it generates the output i want correctly, but i need to write this output to a file so I can call it later using ndiff to compare to another csv file I will run through similar code above

  • 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-06-05T22:25:26+00:00Added an answer on June 5, 2026 at 10:25 pm

    There’s several approaches you can take:

    1. You can either run the program differently; instead of running:

      ./generate
      

      run

      ./generate > output_file.csv
      

      This uses shell redirection to save the standard output to whatever file you specify. This is extremely flexible and very easy to build into future scripts. It’s especially awesome if you accept input on standard input or via a named file, it makes your tool extremely flexible.

    2. You can modify your program to write to a specific file. Open the file with something like:

      output = open("output.csv", "w")
      

      and then write output using strings along the lines of

      output.write(each.name + "," + str(each.produce)  + "," + str(each.amount))
      
    3. You could use the same csv module to also write files. This might be the better approach for long-term use, because it’ll automatically handle complicated cases of inputs that include , characters and other difficult cases.

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

Sidebar

Related Questions

I need to write a C++ code coverage program that takes in another C++
I need to write to a text file using JavaScript. I have a machine
Possible Duplicate: /dev/null in Windows? I have a complex code that encodes input file
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a program that prints 0.(03) for input 1 and 33.
I have a very large binary file and I need to create separate files
I just joined a project, and have been going over the code. We need
I have a flat file that I'm trying to scrub for import into a
I have a php file that writes to a txt file: <?php $answers =
I need to write a program that accepts a basic SQL select statement 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.