pythoncsvsplitreadlines

List index out of range error after trying to read the array from .split() function in python - returns array with only 1 value


I have csv files that I cannot edit from another program. I want ot split the file by the sign ";" using the split function.

1: I open the csv file 2: I use readlines() 3: I use split (;) 4. ERROR when trying to read out splitted value da_split[2] (1 dimensional array is returned)

Code:

`    def get_data_csv(self):

        data = []
        
        f = open("./filename.csv", 'r')
        data = f.readlines()

        print('Printout before splitting:')
        print(data)
        data1 = data

        for da in data1:
            da_split = []
            da_split = da.split(";")
            print("Splitted: ")
            print(da_split)

            self.list_date.append(da_split[0])
            self.list_tempAS.append(self.changeStrtoFloat(da_split[1]))`

Output:

Program Start
Printout before splitting:
['Vorbereitung\n', 'Datum;Uhrzeit;Phase;Screen;Key;Interruptions;Temperatur AS;Temperatur SS;Gewichtswerte AS;Gewichtswerte SS;Info/Comment;\n', 'Nov 18;12:59:06;;;;;;;5613.74g;;;\n', 'Nov 18;12:59:01;;;;;;;5630.78g;;;\n', 'Nov 18;12:59:00;;;;;;;;5657.81g;;\n', 'Nov [...]

Splitted: 
['Vorbereitung\n']
Traceback (most recent call last):
  File "C:\Users\sp7820\PycharmProjects\graphLogger\main.py", line 99, in <module>
    main()
  File "C:\Users\sp7820\PycharmProjects\graphLogger\main.py", line 96, in main
    x.get_data_csv()
  File "C:\Users\sp7820\PycharmProjects\graphLogger\main.py", line 44, in get_data_csv
    self.list_tempAS.append(self.changeStrtoFloat(da_split[1]))
IndexError: list index out of range

Process finished with exit code 1

I tried the str() method but it didn't work anything


Solution

  • Printout before splitting:
      ['Vorbereitung\n',
       'Datum;Uhrzeit;Phase;Screen;Key;Interruptions;Temperatur AS;Temperatur SS;Gewichtswerte AS;Gewichtswerte SS;Info/Comment;\n',
       'Nov 18;12:59:06;;;;;;;5613.74g;;;\n',
       'Nov 18;12:59:01;;;;;;;5630.78g;;;\n',
       'Nov 18;12:59:00;;;;;;;;5657.81g;;\n',
       'Nov [...]
    

    Your first line has no ; in it, therefore splitting it produces just one entry.

    You may want to add a check for len(da_split) to omit such lines automatically:

        for da in data1:
            da_split = []
            da_split = da.split(";")
            print("Splitted: ")
            print(da_split)
    
            if len(da_split) >= 2:
                self.list_date.append(da_split[0])
                self.list_tempAS.append(self.changeStrtoFloat(da_split[1]))
    

    Another option is to use the built-in csv library which avoids doing manual splitting:

        import csv
        f = open("./filename.csv", 'r')
        data = list(csv.reader(f, delimiter=';'))
    

    giving data of:

       [
         ['Vorbereitung'],
         ['Datum', 'Uhrzeit', 'Phase', 'Screen', 'Key', 'Interruptions', 'Temperatur AS', 'Temperatur SS', 'Gewichtswerte AS', 'Gewichtswerte SS', 'Info/Comment', ''],
         ['Nov 18', '12:59:06', '', '', '', '', '', '', '5613.74g', '', '', '']
         ...
       ]