I've watched videos and read text, but I'm still having a hard time grasping classes and their syntax and how they interact. I have the following code that takes in a CSV and puts it into a dict within a list. I want this list to be analyzed in other functions but I don't know how to call it and use it.
How can I use the list from 'data_to_dict' in 'Top_Artists'?
class allData:
def data_to_dict(self):
try:
with open(self.filename, 'r') as data:
header = data.readline().strip().split(',')
entries = []
for row in data:
entry = row.strip().split(',')
listens = {}
for col_pos, col_header in enumerate(header):
listens[col_header] = entry[col_pos]
**return entries**
entries.append(listens)
except IOError:
return "Error."
def Top_Artists():
**entries = self.data_to_dict()**
artist_count = Counter()
for d in entries:
# counts unique artists
arts = d['artist']
artist_count[arts] += 1
Update: I figured out my problem - besides the 'self''s and a return in the data_to_dict, I added:
def __init__(self, filename):
self.filename = filename