索引超出2D数组的范围?

时间:2018-01-02 18:14:46

标签: python

我有一个2D列表,我正在尝试检索索引为spcified作为参数的列(类型:<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:isc="http://extension-functions.intersystems.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="urn:hl7-org:v3"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:variable name="GUID" select="'FF1122'"/> <xsl:template match="ns1:id/@root[.='']"> <xsl:attribute name="root"> <xsl:value-of select="$GUID"/> </xsl:attribute> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> )。 尝试检索除索引IntEnum之外的任何列时,我收到index out of bounds错误。

枚举:

0

以下是 TrafficData

的成员方法

从文件中读取并存储矩阵:

class Column(IntEnum):
    ROAD = 0
    SECTION = 1
    FROM = 2
    TO = 3
    TIMESTAMP = 4
    VFLOW=5 

class TrafficData:
    data=[[]]

检索所需的列:

def __init__(self,file):
    self.data=[[word for word in line.split('\t')]for line  in file.readlines()[1:]]

拨打:      )

def getColumn(self,columnName):
    return [line[columnName] for line in self.data]

我在 column1 = traficdata.getColumn(columnName=Column.ROAD) `column2 = traficdata.getColumn(columnName=Column.FROM)` //error `column3 = traficdata.getColumn(columnName=Column.TO)` //error 处理后附加了一张包含数据的图片: enter image description here

[

1 个答案:

答案 0 :(得分:2)

我测试了您上面提供的代码,但未发现任何问题。这让我相信文件中的数据可能有问题。你能粘贴文件数据吗? (制表符分隔数据)

更新 - 我发现了这个问题 - 怀疑是这是一个数据问题(也涉及一些次要的代码更新)。进行以下更改 -

1)打开文件时使用适当的编码,我使用了utf-16。

2)在您共享的数据文件的末尾,它包含文本 - “(72413行受影响)”以及几个新行字符。因此,您有2个选项,可以手动清理数据文件,也可以更新代码以忽略“(72413行受影响)”&amp; “\ n”字符。

希望有所帮助。