IndexError:读取CSV时字符串索引超出范围

时间:2018-03-21 07:30:46

标签: python-3.x index-error

我对编程非常陌生,并且从一些非常有用的线程拼凑了一些程序,但我有点卡在这里。如果我是个白痴,请先发帖对不起。我看过关于这个主题的帖子,但我不确定答案如何适用于我的情况。我收到以下错误:

  

文件“C:/ Users / david / rest sand.py”,第33行,in     portfolio.append(行[2])   IndexError:字符串索引超出范围

这就是我所拥有的:

import csv
with open('C:/Users/david/OneDrive/Desktop/CSV/Extracted/ENV EXPORT TEST.csv', 'r') as lookuplist:
with open('C:/Users/david/OneDrive/Desktop/CSV/Extracted/RESTRICTION TEST.csv', 'r') as csvinput:
    with open('C:/Users/david/OneDrive/Desktop/CSV/combined.csv', 'w') as output:
        export = csv.reader(lookuplist)
        restrictions = csv.reader(csvinput)
        writer = csv.writer(output)
        d = {}
        for ac in restrictions:
            d[ac[0]] = ac[3:]
        for i in export:
            if i[0] in d:
                i.append(d[i[0]])
            writer.writerow(i)

account = []
portfolio = []
MKT_val = []
closed = []
restriction = []

with open('C:/Users/david/OneDrive/Desktop/CSV/combined.csv') as combinedCSV:
    csvReader = csv.reader(combinedCSV)
    for row in combinedCSV:
        account.append(row[0])
        portfolio.append(row[1])
        MKT_val.append(row[2])
        closed.append(row[3])
        restriction.append(row[4])

for a, b, c, d, e in zip(account,portfolio,MKT_val,closed,restriction):
    while e.startswith('[A1]'):
        print(a,b,c,d,e)

作家CSV出现如下:

Account    Portfolio    Mkt Value   Closed  ['Restriction']

362301370   CIG       24154         YES     ['A1']

917367810   MGI       16509         NO      ['B2']

948462741   FG        26626         YES     ['C3']

我正在寻找的是将第一行CSV打印到控制台。

任何提示将不胜感激。非常感谢!

0 个答案:

没有答案