Python IndexError:索引太多了

时间:2014-05-12 10:25:44

标签: python indexing runtime-error indices

我已经搜索过类似的问题但我无法找到解决方案。在前面我不知道Python。我刚刚得到了一个理论上应该工作的脚本,并用一些点数据绘制了一个图表,但是我得到了这个错误:

Traceback (most recent call last):
File "C:\***\create_plot.py", line 38, in <module>
formatter.create_plot()
File "C:\***\CPI_Plotter.py", line 54, in create_plot
plot(line[:, 0], line[:, 1], styles[name[0]%7], label=name[1])
IndexError: too many indices

适当的代码在这里:

def create_plot(self):
    """
        Plot the different data sets 
    """
    styles = ['o', 's', '^', 'v', 'D', '1', '+']

    for name, line in zip(enumerate(self.labels), self.plot_data):
        if name[1][-4:] == '.csv':
            plot(line[:, 0], line[:, 1], label='XNS Simulation')
            styles.insert(name[0],'')
        else:
            plot(line[:, 0], line[:, 1], styles[name[0]%7], label=name[1])

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

你不能在python中使用逗号进行切片。这个字符串应该做什么?

plot(line[:, 0], line[:, 1], label='XNS Simulation')

答案 1 :(得分:0)

我解决了这个问题。就像已经说过的那样,我认为这是Windows文件系统的问题或类似的问题。在Mac OS上它运行良好。 要在Windows机器上运行,我更改了以下命令

infile = open(filepath, "r")

infile = open(filepath, "rb")

这样,输入文件/文件以二进制模式打开,脚本正常运行。

相关问题