IndexError:索引超出范围:7 [英] IndexError: index out of range: 7

查看:284
本文介绍了IndexError:索引超出范围:7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用称为财务数据质量管理企业版(FDMEE)的Oracle EPM产品.我已经编写了一个Jython脚本来解析数据文件并将其推送到FDMEE产品架构中的自定义表.

I am working with an Oracle EPM Product called Financial Data Quality Management Enterprise Edition (FDMEE). I have written a Jython script to parse a data file and push it to a custom table within the FDMEE product schema.

当我推送数据文件的子集时,它工作正常.但是,当我解析整个数据文件时,它失败并显示错误IndexError:索引超出范围:7.

It works fine when I am pushing a subset of data file. But when I parse the entire data file, it fails with the error IndexError: index out of range: 7.

以下是我收到的错误消息:

Following is the error message I receive :

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

以下是我使用的代码:

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()

推荐答案

显然,受影响的行少于8列.使用try/except块进行调试:

There are obviously fewer than 8 columns for the affected row. Debug using a 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()

这篇关于IndexError:索引超出范围:7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆