尝试从列表中删除项目时 int 不可订阅
int not subscriptable when trying to remove item from list
我正在读取一个文件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',...
我只想从列表中删除这两项,其余的将放入构造函数中
我认为listofItems.remove[0[0:2]]
应该是listofItems.remove[0][0:2]
。
但是,切片会容易很多,例如:
with open("outputfromextract") as f:
contentsofFile = f.read()
individualItem = contentsofFile.split(',')[2:]
我正在读取一个文件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',...
我只想从列表中删除这两项,其余的将放入构造函数中
我认为listofItems.remove[0[0:2]]
应该是listofItems.remove[0][0:2]
。
但是,切片会容易很多,例如:
with open("outputfromextract") as f:
contentsofFile = f.read()
individualItem = contentsofFile.split(',')[2:]