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.
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]
I have a .csv file
You're in luck; CSV support is built right in, via the csv
module.
Do you suggest creating a class dictionary for accessing every instance?
I don't know what you think you mean by "class dictionary". There are classes, and there are dictionaries.
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?
Numbers can't be instance names, but they certainly can be dictionary keys.
You don't want to create "instance names" dynamically anyway (assuming you're thinking of having each in a separate variable or something gross like that). You want a dictionary. So just let the IDs be keys.
I miss pointers! :(
I really, honestly, can't imagine how you expect pointers to help here, and I have many years of experience with C++.
good day. you may make it like this:
>>> class person(object):
... def __init__(self, name):
... self.name = name
...
>>> d = dict()
>>> names = ['1','2','3','4','5']
#fill dict with any value. for example with hash of name
>>> for name in names:
... d[person(name)] = hash(name)
...
>>>
>>> d
{<__main__.person object at 0x000000000236BC18>: 1977051568, <__main__.person ob
ject at 0x0000000002372198>: -1933914571, <__main__.person object at 0x000000000
1E95FD0>: 2105051955, <__main__.person object at 0x0000000002372160>: -180591418
8, <__main__.person object at 0x00000000023721D0>: -2061914958}
>>>
>>> for key in d.keys():
... key.name
...
'1'
'4'
'2'
'5'
'3'
Now We have 5 objects.