IndexError: index out of range: 7

I work with an Oracle EPM product called Financial Data Management (FDMEE). I wrote a Jython script to parse a data file and click it on a user table in the FDMEE product schema.

It works great when I click on a subset of a data file. But when I parse the whole data file, it fails with an IndexError: index out the range: 7 error.

The following is the error message:

File "\\vmhodvesip4\D$\SVESI7\Custom\FDMEEApps\BFRVN/data/scripts/event/BefImport.py", line 5, in <module> if row[7]=='JAN': IndexError: index out of range: 7 

The following is the code I'm using:

 import csv recReader = csv.reader(open('D:/SVESI7/Custom/FDMEEApps/BFRVN/inbox/BF_Reven_Load/Test03big.txt'), delimiter='!') for row in recReader: if row[7]=='JAN': period_num = '1' elif row[7]=='FEB': period_num = '2' elif row[7]=='MAR': period_num = '3' elif row[7]=='APR': period_num = 4 elif row[7]=='MAY': period_num = 5 elif row[7]=='JUN': period_num = 6 elif row[7]=='JUL': period_num = 7 elif row[7]=='AUG': period_num = 8 elif row[7]=='SEP': period_num = 9 elif row[7]=='OCT': period_num = 10 elif row[7]=='NOV': period_num = 11 elif row[7]=='DEC': period_num = 12 else: period_num = 'skip' if period_num != 'skip': params1 = ['batch_plnapps_oi',row[7],period_num,'20' + row[1][-2:],row[2], row[3], row[4], row[5], row[6], row[8], row[9], row[10], row[11], round(row[12],12)] ins_stmt1 = "insert into aif_open_interface(batch_name,period,period_num,year,col03,col04,col05,col06,col07,col09,col10,col11,col12,amount) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)" fdmAPI.executeDML(ins_stmt1,params1,False) fdmAPI.commitTransaction() 
+5
source share
2 answers

There are clearly less than 8 columns for the affected row. Debugging using the try/except block:

 for n, row in enumerate(recReader, start=1): try: month = row[7] except: print('Row {0}: {1}'.format(n, row)) 

As a bonus, here is a more efficient way to write your code:

 months = {'JAN': 1, 'FEB': 2, 'MAR': 3, 'APR': 4, 'MAY': 5, 'JUN': 6, 'JUL': 7, 'AUG': 8, 'SEP': 9, 'OCT':10, 'NOV': 11, 'DEC': 12] for row in recReader: month = row[7] period_num = months.get(month, None) if period_num: params1 = ['batch_plnapps_oi', row[7], period_num, '20' + row[1][-2:], row[2], row[3], row[4], row[5], row[6], row[8], row[9], row[10], row[11], round(row[12], 12)] ins_stmt1 = "INSERT INTO aif_open_interface(batch_name, period, period_num, year, col03, col04, col05, col06, col07, col09, col10, col11, col12, amount) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)" fdmAPI.executeDML(ins_stmt1, params1, False) fdmAPI.commitTransaction() 
+2
source

Without seeing your .csv, we cannot help you very much, but ...

  • Make sure that each line in your csv has the correct format.
  • Make sure the last line in your csv is not just spaces.
  • Look at the optional documentation options for csv.reader , in particular newline=''
0
source

All Articles