Im trying to make a CSV file into a dict but I keep getting IndexError: list index out of range..
The CSV looks like this:
Num Country From To Participants Sports Gold Silver Bronze Total
1 AFG 1936 2016 103 7 2 2
2 ALB 1972 2016 43 7
3 ALG 1964 2016 366 20 5 4 8 17
4 ASA 1988 2016 27 8
5 AND 1976 2016 33 8
6 ANG 1980 2016 163 11
8 ARG 1900 2016 1708 34 21 25 28 74
9 ARM 1996 2016 118 13 2 5 9 16
10 ARU 1988 2016 32 10
12 AUS 1896 2016 3579 37 146 167 191 504
13 AUT 1896 2016 1484 32 25 40 42 107
14 AZE 1996 2016 180 18 7 12 25 44
15 BAH 1952 2016 162 9 6 2 6 14
16 BRN 1984 2016 90 8 1 1 1 3
and Im trying to get this output:
This is my try:
I tried using slicing but that didnt helped
Appreciate any help! TY!
The CSV looks like this:
Num Country From To Participants Sports Gold Silver Bronze Total
1 AFG 1936 2016 103 7 2 2
2 ALB 1972 2016 43 7
3 ALG 1964 2016 366 20 5 4 8 17
4 ASA 1988 2016 27 8
5 AND 1976 2016 33 8
6 ANG 1980 2016 163 11
8 ARG 1900 2016 1708 34 21 25 28 74
9 ARM 1996 2016 118 13 2 5 9 16
10 ARU 1988 2016 32 10
12 AUS 1896 2016 3579 37 146 167 191 504
13 AUT 1896 2016 1484 32 25 40 42 107
14 AZE 1996 2016 180 18 7 12 25 44
15 BAH 1952 2016 162 9 6 2 6 14
16 BRN 1984 2016 90 8 1 1 1 3
and Im trying to get this output:
Output:{'AFG': {'Gold': 0, 'Silver': 0, 'Bronze': 2, 'Total': 2}}
(not only for one country but for all the countries)This is my try:
def summer_olympic_stats(file): summer_olympic_medals = {} with open("summer_olympics_countries.csv", "r") as my_csv: data = my_csv.read().split("\n") Country_index = 0 Gold_index = 0 Silver_index = 0 Bronze_index = 0 Total_index = 0 headings = data[0].split(",") for heading in headings: if "Country" == heading: Country_index = headings.index(heading) elif "Gold" == heading: Gold_index = headings.index(heading) elif "Silver" == heading: Silver_index = headings.index(heading) elif "Bronze" == heading: Bronze_index = headings.index(heading) elif "Total" == heading: Total_index = headings.index(heading) for line in data[1:]: line = line.split(",") summer_olympic_medals.update({ line[Country_index] : { "Gold": line[Gold_index], "Silver": line[Silver_index], "Bronze": line[Bronze_index], "Total": line[Total_index] } }) return summer_olympic_medalsAnd I get the error list index out of range..
I tried using slicing but that didnt helped
Appreciate any help! TY!