سؤال

I am running some functions against a csv file. I call to a specific column and split the text:

 with open('lasty.csv','rb') as f:
     reader = csv.reader(f, delimiter=',')
     for column in reader:
          entry = column[7].split(" ")[4]  

An example of column 7 would look like:

    ['Withdrawal', 'Debit', '', '', "MITTCHELL'S", 'FISH', 'M', '', '', '', '', 'location', 'location', '', 'location']

I am receiving the following error:

    entry = column[7].split(" ")[4]
    IndexError: list index out of range

However if I run:

  entry = column[7].split(" ")[0]

It works with out error, but after [0] it gives me the same error.

Thank you in advanced.

EDIT:

column 7 before splitting looks like:

    "Withdrawal Debit   MITTCHELL'S FISH M     WINTER PARK  FLUS"

In this case I am looking to grab "MITTCHELL'S" which when the column is split is in the [4] or 5th place.

هل كانت مفيدة؟

المحلول

row is an individual row from your input CSV and already split into columns.

You are trying to split an empty string in this case:

>>> ''.split(' ')
['']

Just use entry = row[4] instead:

 for row in reader:
      entry = row[4]  
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top