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

The Archive Base Latest Questions

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

I have a class to import data from a CSV file, and a function

  • 0

I have a class to import data from a CSV file, and a function that takes the filename and a name for the output list. I want to set the name of the self.data_name to be self.info using the setattr() function. How can I do this?

import csv

class import_data:

    def import_csv(self, filename_csv, data_name):


            setattr(self,data_name,0)

            datafile = open(filename_csv, 'r')
            datareader = csv.reader(datafile)
            self.data_name = []

            for row in datareader:
                self.data_name.append(row)
            print("finished importing data")

b = import_data()
b.import_csv('info.csv', 'info')
print(b.info)

This does not work because b.data_name is not b.info. This prints 0 instead of the imported CSV file.

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

    You’re going to have to replace all usages of self.data_name in the import_csv() function with calls to either setattr() or getattr() to be able to use the dynamic name.

    Using self.data_name will use the member named data_name, as I suspect you’ve already realised, and that isn’t what you want to do.

    For example, try the following:

    class import_data:
        def import_csv(self, filename_csv, data_name):
    
                #set dynamic named item to default value
                #not required if this will happen anyway (in this example it does)
                setattr(self,data_name,[])  
    
                #preparation activities
                datafile = open(filename_csv, 'r')
                datareader = csv.reader(datafile)
    
                #do required work using a temporary local variable
                temp = []
                for row in datareader:
                    temp.append(row)
    
                #copy the temporary local variable into the dynamically named one
                setattr(self, data_name, temp)
    
                #tidy up activities
                datafile.close()
                print("finished importing data")
    

    Make sure you take a look at eumiro‘s answer, which takes a better, more compact and more Pythonic approach to your specific problem using with and list(). However, the above should hopefully make it clear how you could be using setattr() in a wider variety of cases.

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

Sidebar

Related Questions

I have class with a member function that takes a default argument. struct Class
I have a web form used for importing data from a CSV file. It
In my application i can import csv file but i have data in excel
I have class method that returns a list of employees that I can iterate
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have an application that takes some input and generates configuration files as output.
I had an application that was downloading a .CSV file from a password-protected website
Here, i have coded to get data from DB. I want to store the
I have a form in my forms.py that looks like this: from django import
I have a large file, with 1.8 million rows of data, that I need

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.