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()

  • 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? – OneCricketeer Oct 21 '19 at 04: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. – thevioletsaber Oct 21 '19 at 04:37
  • 1
    Might want to look at https://stackoverflow.com/questions/27123125/sorting-a-text-file-alphabetically-python – OneCricketeer Oct 21 '19 at 04: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. – Christopher Oct 21 '19 at 04: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 –  Oct 21 '19 at 04:54
  • @Christopher said it more clearly than I did. If you're having trouble with that specific approach then feel free to post the code you've written so far and what error specifically you're running into. – thevioletsaber Oct 21 '19 at 04:57

1 Answers1

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
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 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 –  Oct 21 '19 at 05:12
  • 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. – martineau Oct 21 '19 at 05:26