Search code examples
python-2.7listsubscript

int not subscriptable when trying to remove item from list


I'm reading a file outputfromextractand I want to split the contents of that file with the delimiter ',' which I have done.

When reading the contents into a list there's two 'faff' entries at the beginning that I'm just trying to remove however I find myself unable to remove the index

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)

In the file I have created the first three lines are enclosed below for reference:

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

I'm wanting to simply remove those two items from the list and the rest will be put into an constructor


Solution

  • I believe listofItems.remove[0[0:2]] should be listofItems.remove[0][0:2].

    But, slicing will be much easier, for example:

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