Skip to content Skip to sidebar Skip to footer

Search A Data.txt File For Information Based On User Input And Print It(python 3.5.1)

the code below reads the data.txt file and prints the records in the data.txt file. text_file = open('data.txt', 'r') lines = text_file.readlines() print (lines) print (lines) te

Solution 1:

I'm assuming that you use Python 3.

defprint_entries(date):
    """Prints all the entries that match with date"""withopen('a.txt', 'r') as f:
        flag = False
        content = f.readlines()
        content = [line.strip('\n').split(',') for line in content]
        for row in content:
            if row[0] == date:
                flag = Trueprint(*row, sep='\t')
        ifnot flag:
            print('Try again')
        return flag


whilenot print_entries(input("Enter date :")):
    pass

If you're using Python 2, replace print(*row, sep = '\t') with print('\t'.join(row)).

Running the program -

Enter date :12-4-2014
12-4-2014   Glenview    21922.22    17

Post a Comment for "Search A Data.txt File For Information Based On User Input And Print It(python 3.5.1)"