I have an txt file listing name and age:
John,20
Mary,14
Kevin,60
Mary,15
John,40
And trying to write the below function to return a dictionary:
def read(filename):
results = {}
with open(os.path.join(os.path.dirname(__file__), 'data.txt')) as file:
for line in file:
location,value = line.split(',', 1)
results[location] = value
print(results)
I'm trying to format as:
{'John': [20, 40], 'Mary': [14, 15], 'Kevin': [60]}
But currently getting:
{'John': '20', 'Mary': '15\n', 'Kevin': '60\n'}
Can anyone help me understand what I'm doing wrong?