I am trying to read in the following text file with four columns separated by commas:
1, 0.1, 0.2, 73
1, 0.11, 0.1, 101
2, 0.23, 0.01, 17
2, 0.12, 0.15, 23
where the first number represents a batch, the second and third represent an x and y coordinate respectively and the fourth is a value.
And I want to save this text file as a dictionary in python. I tried using a save_rows function to create a dictionary:
def save_rows('sample1.txt'):
keys = ['batch', 'x', 'y', 'val']
dicts = []
with open(filename) as f:
for line in f:
line = line.strip().split(',')
d = dict(zip(keys, line))
return d
dicts.append(d)
But it doesn't return or create an dictionary, what am I doing wrong?
Thanks in advance!