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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:55:18+00:00 2026-05-13T09:55:18+00:00

I am still struggling with understanding classes, I am not certain but I have

  • 0

I am still struggling with understanding classes, I am not certain but I have an idea that this function I have created is probably a good candidate for a class. The function takes a list of dictionaries, identifies the keys and writes out a csv file.

First Q, is this function a good candidate for a class (I write out a lot of csv files

Second Q If the answer to 1 is yes, how do I do it

Third Q how do I use the instances of the class (did I say that right)

import csv
def writeCSV(dictList,outfile):
    maxLine=dictList[0]
    for item in dictList:
        if len(item)>len(maxLine):
            maxLine=item
    dictList.insert(0,dict( (key,key) for key in maxLine.keys()))

    csv_file=open(outfile,'ab')
    writer = csv.DictWriter(csv_file,fieldnames=[key for key in maxLine.keys()],restval='notScanned',dialect='excel')
    for dataLine in dictList:
        writer.writerow(dataLine)
    csv_file.close()
    return
  • 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-13T09:55:18+00:00Added an answer on May 13, 2026 at 9:55 am

    If you want to write a lot of CSV files with the same dictList (is that what you’re saying…?), turning the function into a class would let you perform initialization just once, and then write repeatedly from the same initialized instance. E.g., with other minor opts:

    class CsvWriter(object):
    
      def __init__(self, dictList):
        self.maxline = max(dictList, key=len)
        self.dictList = [dict((k,k) for k in self.maxline)]
        self.dictList.extend(dictList)
    
      def doWrite(self, outfile):
        csv_file=open(outfile,'ab')
        writer = csv.DictWriter(csv_file,
                                fieldnames=self.maxLine.keys(),
                                restval='notScanned',
                                dialect='excel')
        for dataLine in self.dictList:
            writer.writerow(dataLine)
        csv_file.close()
    

    This seems a dubious use case, but if it does match your desire, then you’d instantiate and use this class as follows…:

    cw = CsvWriter(dataList)
    for ou in many_outfiles:
      cw.doWrite(ou)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with this yesterday whole day, and still couldnt figure it
While I'm still struggling to find a solution for this question, i have another
I'm still struggling with this concept. I have two different Person objects, very simply:
still struggling with regex :) i have this code: $link = '/\bhttp:\/\/.+\..+[\/\w]?\b/i'; $match =
I fear this has been beaten to death, but I'm still struggling with the
I'm still struggling a bit with the idea of ARC. Let's suppose I have
I see a number of posts on this, but I'm still struggling. I've got
I've been reading up on how this works but am struggling to make sense
I've tried to install Matplot lib but I'm still struggling to get past errors.
This is a rather basic question, however I'm still struggling with it a little.

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.