0

in python 2.7 this section is fine. It opens and reads a csv file, sorts and then writes to a final file.

data = csv.reader(open('H:\\python code\\hpsm_file1.csv'),delimiter=',')
sortedlist = sorted(data, key=operator.itemgetter(3)) 

in python 3.7 it fails with this error. There are no other changes other than the version of python has changed

IndexError: list index out of range

rambi
  • 1,013
  • 1
  • 7
  • 25
kevin wholley
  • 85
  • 1
  • 8
  • Maybe the problem is related to csv reader. Look at the answer on https://stackoverflow.com/questions/5180555/python-2-and-3-csv-reader – Vítor Cézar Feb 26 '20 at 17:09
  • But the full traceback might also help. – Jongware Feb 26 '20 at 17:22
  • I'm not really sure this address my issue. in 2.7 it is fine in 3.7 that one command is broken. I did not open in binary or utf just open with (w) I can't see how to perform the same result in 3.7? – kevin wholley Feb 26 '20 at 17:23
  • The linked question also states "Also in Python 3 newline='' should be specified" - iirc, leaving this out ends up returning empty lists wherever there would be a line break. This assumption coincides with your error since you're using `operator.itemgetter(3)` as your sort key, which would fail if your data contains an empty list (or a list with >4 elements for that matter). – b_c Feb 26 '20 at 17:32
  • 1
    I get this error a lot in file handling if the final line has a newline charecter with no information after it. I would probably split up that line in to multiple lines with checks for each step to make sure everything is happening as expected. – Parcevel Feb 26 '20 at 18:04
  • Please share the entire error message, as well as a [mcve]. – AMC Feb 26 '20 at 18:06
  • Ok thanks, so do I understand the change in 3.* of python went from the commands I had above to this data = csv.reader(open('H:\\python code\\hpsm_file1.csv', 'r', newline='', encoding='utf8') sortedlist = sorted(data, key=operator.itemgetter(3)) – kevin wholley Feb 26 '20 at 18:13
  • I encountered the same problem as @Parcevel when I had a CSV file with empty lines somewhere. – abaumg Nov 11 '21 at 09:34

0 Answers0