Let's say I have a dataset ('test.csv') like so:
Name,Fruit,Price
John,Apple,1.00
Steve,Apple,1.00
John,Mango,2.00
Adam,Apple,1.00
Steve,Banana,1.00
Although there are several easier ways to do this, I would like to organize this information as a class in python. So, ideally, an instance of a class would look like:
{'name': 'John', 'Fruits': ['Apple','Mango'], 'Price':[1.00, 2.00]}
My approach to loading the dataset into a class is to store each instance in a list.
class org(object):
def __init__(self,name,fruit,price):
self.name = name
self.fruit = [fruit]
self.price = [price]
classes = []
with open('test.csv') as f:
for line in f:
if not 'Name' in line:
linesp=line.rstrip().split(',')
name = linesp[0]
fruit = linesp[1]
price = linesp[2]
inst = org(name,fruit,price)
classes.append(inst)
for c in classes:
print (c.__dict__)
In this case, how do I know if 'John' already exists as an instance?
If so, how do I update 'John'? With a classmethod?
@classmethod
def update(cls, value):
cls.fruit.append(fruit)