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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:39:02+00:00 2026-06-03T18:39:02+00:00

I apologise if this question has already been asked. I’m really new to Python

  • 0

I apologise if this question has already been asked.

I’m really new to Python programming, and what I need to do is this:
I have a .csv file in which each line represent a person and each column represents a variable.
This .csv file comes from an agent-based C++ simulation I have done.
Now, I need to read each line of this file and for each line generate a new instance of the class Person(), passing as arguments every variable line by line.

My problem is this: what is the most pythonic way of generating these agents while keeping their unique ID (which is one of the attributes I want to read from the file)? Do you suggest creating a class dictionary for accessing every instance? But I still need to provide a name to every single instance, right? How can I do that dynamically? Probably the best thing would be to use the unique ID, read from the file, as the instance name, but I think that numbers can’t be used as instance names, can they? I miss pointers! 🙁

I am sure there is a pythonic solution I cannot see, as I still have to rewire my mind a bit to think in pythonic ways…
Thank you very much, any help would be greatly appreciated!
And please remember that this is my first project in python, so go easy on me! 😉

EDIT:
Thank you very much for your answers, but I still haven’t got an answer on the main point: how to create an instance of my class Person() for every line in my csv file. I would like to do that automatically! Is it possible?
Why do I need this? Because I need to create networks of these people with networkx and I would like to have “agents” linked in a network structure, not just dictionary items.

  • 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-03T18:39:06+00:00Added an answer on June 3, 2026 at 6:39 pm

    For reading the csv file and generate a dict file from it you should definitively have a look at http://docs.python.org/library/csv.html#csv.DictReader.

    If you have a csv file that has as a first row the name of the fields and the data on the rest of the rows, DictReader would generate a dictionary taking as keys the name of the fields defined in the first row. To give an example if you give it the following csv file:

    Field1,Field2,Field3
    1,2,3
    4,5,6
    

    It would return the following list:

    [{'Field1':1,'Field2':2,'Field3':3} , {'Field1':4,'Field2':5,'Field3':6} ]
    

    ADDED:

    Regarding the creation of the instances based on the dictionary you should probably have a look at the following:

    Creating class instance properties from a dictionary in Python

    Or taken from the following link you can do the following:

    class Person:
        def __init__(self, **kwds):
            self.__dict__.update(kwds)
    
    # that's it!  Now, you can create a Bunch
    # whenever you want to group a few variables:
    
    person = Person(field1=1, field2=2, field3=3)
    

    Where you could use every entry of the dictionary returned by the dictReader to create a new instance:

    reader = csv.DictReader(open('yourfile.csv', 'rU'), delimiter=',')
    entries = [Person(**entry) for entry in reader]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologise if this question has already been asked on here before but I
Apologies if this question has been asked already, but suppose we have this code
If this question has already been asked I appologies, please point me in the
I feel like this question has probably been asked a thousand times already, so
Apologies if this question has already been asked but I do not think I
I am quite sure this question has already been asked several times and I
Apologies if this is too ignorant a question or has been asked before. A
I apologize if this has already been asked a different way but I couldn't
I apologize if this question has been answered already, but I cannot seem to
I apologise if this has been asked, my searches only found bits and pieces...

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.