0

I have been trying this for a few days now. I have to read in a file containing zoo animals. (i.e. ['ID', 'Name', 'Species']) Next, have to accept the user's ID choice and delete from the list. This is what I so far. I am stuck and can't proceed further until this section is complete. I appreciate any comments. Also using python 3.

f = open('ZooAnimals.txt', 'r') #read in file
file = [line.split(',') for line in f.readlines()] #turn file into list
c = input("What ID would you like to delete? ") #accept user input
file1 = list(c)#turn user input into a list item
list.pop(file1) #suppose to pop out the value
print(file)

EDIT:

The file contains the following items for example. [['1', 'Whiskers', 'Siberian Tiger\n'], ['2', 'Babbity', 'Belgian Hare\n'], ['3', 'Hank', 'Spotted Python\n'], ['17', 'Larry', 'Lion\n'], ['10', 'Magilla', 'Eastern Gorilla\n'], ['1494', 'Jim', 'Grizzy Bear\n']]

I want to try and delete for example, ID 2, Babbity, Belgian Hare

This is what I can't do with my current code

3
  • What is the issue you are having? Commented Apr 3, 2015 at 1:31
  • I cannot delete the ID from the file I am reading in Commented Apr 3, 2015 at 1:31
  • Do you have to use a nested list (e.g. for an assignment), or can you use a more appropriate data structure? Commented Apr 3, 2015 at 1:32

2 Answers 2

4

list.pop accepts an index as an argument. For lists, this can only be an integer. Also, list('abc') != ["abc"], but rather ["a", "b", "c"] because of str's iteration protocol (it goes by letter).

with open("ZooAnimals.txt") as zoo_animals_txt:
    # [("123", "cat", "felis catus"), ...]
    animals = [line.split(",") for line in zoo_animals_txt]

user_input = input("What ID would you like to delete? ")

for index, (id_, name, species) in enumerate(animals):
    if id_ == user_input:
        animals.pop(index)
        break

print("The remaining animals are:")
print(*animals, sep="\n")

Then, to update the file with the change:

with open("ZooAnimals.txt", "w") as zoo_animals_txt:
    for animal_stats in animals:
        print(",".join(animal_stats), file=zoo_animals_txt)
Sign up to request clarification or add additional context in comments.

4 Comments

Did you also want this change reflected in the file?
I was about to figure that out. Would i change "with open("ZooAnimals.txt") as zoo_animals_txt:" to "with open("ZooAnimals.txt", 'w+') as zoo_animals_txt:" EDIT: nevermind that totally erases the file
I added a way to update the file in my answer. Edit: Also, w+ as the mode for opening the file truncates (erases) the file first, while r+ will not. I chose to just reopen the file.
@SethBulliard please give me him an upvote and add it as answer.
0

list.pop() removes the last element of a list. You probably want file.remove(c). Also, remove file1 = list(c). I'm not sure why that's there.

2 Comments

TypeError: descriptor 'remove' requires a 'list' object but received a 'str'
The ID isn't the whole item. The file is turned into a list with nested lists. For list.remove(c) to work (although I seriously wouldn't recommend overriding the buil-in list), c would have to be something like ['3', 'Bob', 'Lion']. Also, pop() can remove and return a specific element of a list if you pass it the index, e.g. [1,2,3].pop(2) returns 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.