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]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…