Python Forum
List index out of range when turning CSV into dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List index out of range when turning CSV into dict
#1
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:
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_medals
And I get the error list index out of range..
I tried using slicing but that didnt helped
Appreciate any help! TY!
Reply


Messages In This Thread
List index out of range when turning CSV into dict - by ranbarr - May-10-2021, 01:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 6,152 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,372 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,948 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,946 May-14-2021, 07:14 AM
Last Post: perfringo
  List vs index: Frederico_Caldas 5 3,646 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,299 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,101 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  Dict from list - HELP! PLEASE! cherry_cherry 16 5,655 Apr-09-2020, 04:01 AM
Last Post: cherry_cherry
  list index out of range mcgrim 2 2,939 May-25-2019, 07:44 PM
Last Post: mcgrim
  Turning column of 8 digit numbers into three seperate columns Wade334 1 2,192 May-11-2019, 02:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020