Search code examples
pythonfor-loopreadlines

printing lines from a file within an inclusive alphabetical range not including desired limits


The trouble I'm having with this program is that it is not including the bounds even though I use the >= <= operators. Also for some reason the words that are output are each separated by a newline rather than printing one after another.

For example, if the chosen .txt file contains:

Aladdin
Batman
Dinosaurs
Edgar
Fruitloop
Mongoose

and the chosen upper and lower bounds are:

Batman
Fruitloop

The program prints:

Batman

Dinosaurs

Edgar


Here is what I'm working with. Any help is greatly appreciated!

import os

user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
lo_limit = input() #reads a user chosen word as the inclusive lower alphabetical limit
up_limit = input() #reads a user chosen word as the inclusive upper alphabetical limit

file_handle = open(user_file) #opens user chosen file

lines = file_handle.readlines() #creates by-line string of file contents


#if user chosen file contains words equal to or between bounds, prints words
for ln in lines:
    if ln >= lo_limit \
        and ln <= up_limit:
        print(ln)

Solution

  • That happens because when you do f.readlines() this will return a list like this:

    f.readlines()
    >>>['Aladdin\n', 'Batman\n', 'Dinosaurs\n', 'Edgar\n', 'Fruitloop\n', 'Mongoose']
    

    And when you enter up_limit=Edgar, you will be comparing each of the list f.readlines() with the word Edgar like this:

    'Aladdin\n'>=lo_limit and 'Aladdin\n'<='Edgar'
    >>>True
    'Batman\n'>=lo_limit and ''Batman\n''<='Edgar'
    >>>True
    ....
    ....
    ....
    

    And when becomes the iteration of 'Edgar\n' you can check that:

    'Edgar'>='Edgar\n'
    Out[6]: False
    

    And that's why 'Edgar' is not printed. You could try:

    import os
    
    user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
    lo_limit = input() #reads a user chosen word as the inclusive lower alphabetical limit
    up_limit = input() #reads a user chosen word as the inclusive upper alphabetical limit
    
    with open(str(user_file)) as file_handle:#opens user chosen file
        lines = file_handle.readlines()
        #if user chosen file contains words equal to or between bounds, prints words
        for ln in lines:
            if (ln > lo_limit) or (ln == lo_limit) or (ln < up_limit):
                print(ln)
                if (ln == up_limit+'\n'):
                    break
    

    Or you can select by index:

    user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
    lo_limit = str(input()) #reads a user chosen word as the inclusive lower alphabetical limit
    up_limit = str(input()) #reads a user chosen word as the inclusive upper alphabetical limit
    
    with open(str(user_file)) as file_handle:#opens user chosen file
        lines = file_handle.readlines() #creates by-line string of file contents    
        linesselected=lines[lines.index(lo_limit+'\n'):(lines.index(up_limit+'\n')+1)]
        for i in linesselected:
            print(i.replace('\n',''))