【问题标题】:Python IndexError: too many indicesPython IndexError:索引过多
【发布时间】:2014-05-12 10:25:43
【问题描述】:

我已经搜索过类似的问题,但找不到解决方案。在前面我对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])

感谢您的帮助!

【问题讨论】:

    标签: python indexing runtime-error indices


    【解决方案1】:

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

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

    【讨论】:

    • 据我理解的代码,这应该命名图形和图中显示的不同点数据。
    • 你能展示 self.labels 和 self.plot_data 的例子吗?
    • 我做了一些测试,脚本在 Mac OS 上运行良好。所以它必须是windows的错误,或者更准确地说脚本中的某些东西在windows上不起作用。也许它与Windows上的文件系统有关?!
    【解决方案2】:

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

    infile = open(filepath, "r")
    

    进入

    infile = open(filepath, "rb")
    

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

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 2020-04-10
      • 1970-01-01
      • 2020-09-26
      • 2019-07-17
      • 2020-09-14
      • 2014-04-02
      相关资源
      最近更新 更多