I have a file of the following format:
a 1
a 2
a 3
b 4
b 5
b 6
c 7
c 8
c 9
Here is my code:
def file_to_dict(fname):
f = open("file.txt")
d = {}
for line in f:
columns = line.split(" ")
letters = columns[0]
numbers = columns[1].strip()
d[letters] = list(numbers)
print d
if __name__ == "__main__":
fname = "file.txt"
The output must be {"a": ["1", "2", "3"], "b": ["4", "5", "6"], "c": ["7", "8", "9"]}. But my output shows only the last repeated key and its value, i.e. {"a": ["3"], "b": ["6"], "c": ["9"]}. Can you help?