CSV文件,错误。蟒蛇

时间:2018-06-13 14:16:50

标签: python csv

这是我的代码:

import numpy as np
import csv
import logging

Buy  = 'Buy'
Sell = 'Sell'

csvFile = 'C:/Users/Usuario/Desktop/compu/GFG.csv'

class MovingAverage:
    """
    Parámetros:
    :_data:          CSV con los datos de instrumento.
    :_shortTerm:     Tamaño de plazo largo en días.
    :_longTerm:      Tamaño de plazo corto en días.
    """
    def __init__(self, _data=csvFile, _shortTerm=10, _longTerm=15):
        self.closingPrices = []

        with open(csvFile,'r') as CSVFile:
            reader = csv.reader(CSVFile)
            for row in reader:
                self.closingPrices.append(row[1]) # Tomo los close
        self.closingPrices.pop(0)    # borro primera fila (date)
        self.closingPrices = map(float, self.closingPrices)
        self.data =[]
        self.shortTerm = _shortTerm
        self.longTerm = _longTerm
        self.shortAverage = self.setShortAverage()
        self.longAverage = self.setLongAverage()
        self.lastIndicator = None
        self.init_logging()

然后,我有这个错误:

  File "C:/Users/Usuario/Desktop/algotrade/medias.py", line 32, in __init__
    self.closingPrices.append(row[1])    # Tomo los close

IndexError: list index out of range

出了什么问题?这是我的错误?请帮忙!

1 个答案:

答案 0 :(得分:0)

有些时候,row[1]中没有任何值,您最好检查是否有值,然后append

for row in reader:
    if row[1]:
        self.closingPrices.append(row[1])