Skip to content Skip to sidebar Skip to footer

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 a

Solution 1:

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:

classPerson: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]

Solution 2:

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++.

Solution 3:

good day. you may make it like this:

    >>> classperson(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>: -1805914188, <__main__.person object at 0x00000000023721D0>: -2061914958}
    >>>

    >>> for key in d.keys():
    ...     key.name
    ...
    '1''4''2''5''3'

Now We have 5 objects.

Post a Comment for "How Can I Dynamically Generate Class Instances With Single Attributes Read From Flat File In Python?"