Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

oop - How can I dynamically generate class instances with single attributes read from flat file in Python?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...