Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#4
since you only use pandas to read the file, perhaps a better solution would be:
from pathlib import Path
import csv
from CreateDict import CreateDict
import os

class DataPlot:
    def __init__(self):
        # Set starting directory same as code
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        self.cd = CreateDict()
        homepath = Path('.')
        self.infile = homepath / '01011900-30092016_AllData.txt'
    
    def dispatch(self):
        data = self.read_data()
    
    def read_data(self):
        with self.infile.open() as fp:
            crdr = csv.DictReader(fp, delimiter=';')
            for n, row in enumerate(crdr):
                # Replace print statement with your code for each row
                print(f"\n{n}: {self.cd.display_dict(row)}")


def main():
    dp = DataPlot()
    dp.dispatch()


if __name__ == '__main__':
    main()
This will read each row of the csv file into a dictionary format like:
Output:
AgencyID: Ambraseys-Finkel Time: 18.01.1900 15:30:00 Day: 18 Month: 01 Year: 1900 Hour: 15 Minute: 30 Second: 0 MiliSecond: 0 Latitude: 37.7900 Longitude: 28.2100 Depth: -10.0 Fixed: * Mtype: MS Magnitude: 4.40 M0: Rms: Herr: Verr: Gap: StationNO: Country: TÜRKÝYE City: AYDIN District: Yenipazar Town: Other: EventType: Eq
you will need CreateDict.py:
import os
 
class CreateDict:
    """
    Generic Software tools used by Trailmapper.
 
    CreateDict.py - Contains methods to simplify node and cell creation within
                    a dictionary
 
    Usage: 
     
        The best way to learn what can be done is to examine the testit function
        included in this module.
 
        new_dict(dictname) - Creates a new dictionary instance with the name
            contained in dictname
 
        add_node(parent, nodename) - Creates a new node (nested dictionary)
            named in nodename, in parent dictionary.
 
        add_cell(nodename, cellname, value) - Creates a leaf node within node
            named in nodename, with a cell name of cellname, and value of value.
 
        display_dict(dictname) - Recursively displays a nested dictionary.
 
    Requirements:
 
        Trailmapper software:
            None
 
        Python standard library:
            os
     
    Author: Larz60+  -- May 2019.
    """
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
 
    def new_dict(self, dictname):
        setattr(self, dictname, {})
 
    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node
 
    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell
 
    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1
 
 
def testit():
    # instantiate class
    cd = CreateDict()
 
    # create new dictionary named CityList
    cd.new_dict('CityList')
 
    # add node Boston
    boston = cd.add_node(cd.CityList, 'Boston')
    # add sub node Resturants
    bos_resturants = cd.add_node(boston, 'Resturants')
 
    # Add subnode 'Spoke Wine Bar' to parent bos_resturants
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')
 
    # Add subnode 'Highland Kitchen' to parent bos_resturants
    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')
 
    # display dictionary
    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')
 
if __name__ == '__main__':
    testit()
Reply


Messages In This Thread
IndexError: list index out of range - by rf_kartal - Sep-07-2021, 08:12 AM
RE: IndexError: list index out of range - by Larz60+ - Sep-07-2021, 11:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,303 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,514 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,636 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,981 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,406 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,486 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,688 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,291 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Python Error List Index Out of Range abhi1vaishnav 3 2,381 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,257 Jun-22-2021, 10:47 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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