I must extract with Python all the lines at index 1 and 5 of from a CSV file and store them in the dictionary "names". But, since I am new in Python, I am really struggling with thisk task. This is my code:
names = {}
with open("../data/names.csv") as file:
for line in file:
print(line)
output:
Id,Name,Year,Gender,State,Count
1,Mary,1910,F,AK,14
2,Annie,1910,F,AK,12
3,Anna,1910,F,AK,10
4,Margaret,1910,F,AK,8
5,Helen,1910,F,AK,7
6,Elsie,1910,F,AK,6
...
If I try to store the index 1 (the string "Name") and the index 5 (int) of the lines in the dictionary "names", I obtain a wrong output. Here the code:
names = {}
with open("../data/names.csv") as file:
for line in file:
names = {"name": line[1], "count": line[5]}
print(names)
The (wrong) output:
{'name': '6', 'count': '2'}