1

I'm trying to alphabetize items in a list while removing commas so as to format the lines from the list (text file) I'm using.

i'm required to use the while loop and not sure how i can incorporate the sort function to get the lines in alphabetical order

# open file

try:
    f = open('animals.txt')
    print('Success, file has been read\n')
except Exception:
    print('Sorry. This file does not exist')


# display lines

print('Name\tPhylum\tDiet')
print('----\t------\t----')

inFile = open('animals.txt', 'r')
line = inFile.readline()
while (line):
    print(line, end='')
    line = inFile.readline()

6
  • 1
    I'm not sure I understand why you're required to use a while loop, but it wouldn't help you sort anything. Why not read the file into a list first? Commented Oct 21, 2019 at 4:36
  • 2
    Your question specifies you're trying to sort items (lines from a file) in a list. To this end, it seems that adding each line to a list and then sorting the list may be a good approach. Commented Oct 21, 2019 at 4:37
  • 1
    Might want to look at stackoverflow.com/questions/27123125/… Commented Oct 21, 2019 at 4:45
  • 1) Load file into a list in memory 2) Use a loop to sort said list 3) The Assignment requires you to use the while loop, as opposed to all alternatives. | This does sound a lot like a Homework assignment, and with those we are very carefull not to provide code. The trying (and failing) is part of the learning process. Commented Oct 21, 2019 at 4:52
  • @thevioletsaber do you mean sort the list before I work with it on Python? I'm not sure if this is what you mean Commented Oct 21, 2019 at 4:54

1 Answer 1

0

I'm not sure what list you're talking about.
You can sort all the items by creating a list of them like this:

with open('animals.txt', 'r') as inFile:
    animals = []
    line = inFile.readline()
    while line:
        items = line.rstrip().split(',')
        animals.append(items)
        line = inFile.readline()

animals.sort()

print('Name\tPhylum\tDiet')
print('----\t------\t----')

for row in animals:
    print('\t'.join(row))

Output:

Name    Phylum  Diet
----    ------  ----
Bear    Mammal  Omnivore
Bobcat  Mammal  Carnivore
Caiman  Reptile Carnivore
Cheetah Mammal  Carnivore
Croc    Reptile Carnivore
Eagle   Bird    Carnivore
Elk     Mammal  Herbivore
Emu     Bird    Omnivore
Ermine  Mammal  Carnivore
Ibis    Bird    Carnivore
Iguana  Reptile Herbivore
Lizard  Reptile Omnivore
Llama   Mammal  Herbivore
Parrot  Bird    Herbivore
Racoon  Mammal  Omnivore
Turtle  Reptile Omnivore
Yak     Mammal  Herbivore
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, This worked to alphabetize the items in each line. I was looking to alphabetize animal names so the lines would start with Bear, then Bobcat, Caiman and so on.(followed by phylum and diet i.e Bear, Mammal, Omnivore if possible
Isabel: Oh, OK...in that case you have to create a list of them all first and then sort the whole thing. See updated answer. Note that using while line: isn't very "pythonic". Using for line in inFile: would be.

Your Answer

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