Search code examples
python-3.xfilesortinginputsentence

Sorting sentences of text file by users input


My code is sorting the sentences of file based on a length of the sentences by their length and saving to a new file.

How can I alter my code so that if the user inputs any number at program start, we filter the lines based on that input.

Example: The user inputs 50 - the program will sort all sentences that have a greater length than 50 or if the user inputs all then the program will sort all lines as normal.

My code:

file = open("testing_for_tools.txt", "r")
lines_ = file.readlines()
#print(lines_)
user_input = input("enter")
if user_input is int:
    lines = sorted(lines_, key=len)
else:
    lines = sorted(lines_, key=len)
# lines.sort()
file_out = open('testing_for_tools_sorted.txt', 'w')
file_out.write(''.join(lines))  # Write a sequence of strings to a file
file_out.close()
file.close()
print(lines)

Solution

    • input returns a string, always, if you want an integer or somesuch you need to parse it explicitely, you will never get an integer out of input.
    • is is not a type-testing primitive in python, it's an identity primitive. It checks if the left and right are the same object and that's it.
    • filter is what you're looking for here, or a list comprehension: if the user provided an input and that input is a valid integer, you want to filter the lines to only those above the specified length. This is a separate step from sorting.

    That aside,

    • you should use with to manage files unless there are specific reasons that you shan't or can't
    • files have a writelines method which should be more efficient than writing joined lines
    • never ever open files in text mode without providing an encoding, otherwise Python asks the system for an encoding and it's easy for that system to be misconfigured or oddly configured leading to garbage inputs
    with open("testing_for_tools.txt", "r", encoding='utf-8') as f:
        lines_ = file.readlines()
    
    #print(lines_)
    user_input = input("enter")
    if user_input:
        try:
            limit = int(user_input.strip())
        except ValueError:
            pass
        else:
            lines_ = (l for l in lines_ if len(l) >= limit)
    
    lines = sorted(lines_, key=len)
    with open('testing_for_tools_sorted.txt', 'w', encoding='utf-8') as f:
        f.writelines(lines)
    print(lines)