尝试从列表中删除项目时int不可订阅

时间:2017-12-10 13:37:35

标签: python-2.7 list subscript

我正在读取一个文件outputfromextract,我想用分隔符','分割该文件的内容。

当将内容读入列表时,开头有两个'faff'条目,我只是想删除但是我发现自己无法删除索引

import json

class device:
   ipaddress = None
   macaddress = None
   def __init__(self, ipaddress, macaddress):
       self.ipaddress = ipaddress
       self.macaddress = macaddress

listofItems = []
listofdevices = []


def format_the_data():
    file = open("outputfromextract")
    contentsofFile = file.read()
    individualItem = contentsofFile.split(',')
    listofItems.append(individualItem)
    print(listofItems[0][0:2]) #this here displays the entries I want to remove
    listofItems.remove[0[0:2]] # fails here and raises a TypeError (int object not subscriptable)

在我创建的文件中,前三行包含在下面以供参考:

[u' #created by system\n', u'time at 12:05\n', u'192.168.1.1\n',...

我想简单地从列表中删除这两个项目,其余的将被放入构造函数中

1 个答案:

答案 0 :(得分:1)

我相信listofItems.remove[0[0:2]]应为listofItems.remove[0][0:2]

,切片会更容易,例如:

with open("outputfromextract") as f:
    contentsofFile = f.read()
    individualItem = contentsofFile.split(',')[2:]