Saturday, March 2, 2019

How to Save, Fetch & Search Data From Text File - Python3

f = open("text_search.txt", "a")
n = int(input("Enter the number of students you want to enter: "))
for i in range(n):
    en = input("Enter the Enrollment number of student {}: ".format(i+1))
    name = input("Enter the Name of student {}: ".format(i+1))
    age = input("Enter the Age of student {}: ".format(i+1))
    email = input("Enter the Email id of student {}: ".format(i+1))
    phone = input("Enter phone_no of student {}: ".format(i+1))
    f.write(str(en)+"," + name + "," +str(age) + "," + email + "," +str(phone)+"\n")
f.close()

f = open("text_search.txt", 'r')
list1 = list(f)
print (list1)
print("-"*100)
print("|{:^20}{:^20}{:^20}{:^20}{:^20}|".format('ENROLLMENT_NO', 'NAME', 'AGE','E-MAIL', 'PHONE-NO'))
print("-"*100)
for line in list1:
    info = ""
    a = line.split(",")
    for i in a:
        info += "{:^20}".format(i)
    print(info)
print("-"*100)
f.close()

term=input("Enter a keyword you want to search : ")
print("-"*100)
print("|{:^20}{:^20}{:^20}{:^20}{:^20}|".format('ENROLLMENT_NO', 'NAME', 'AGE','E-MAIL', 'PHONE-NO'))
print("-"*100)
f = open("text_search.txt")
for line in f:
    line.strip().split('/n')
    if term in line:
        info = ""
        a = line.split(",")
        for i in a:
            info += "{:^20}".format(i)
        print(info)
f.close()

ScreenShot:


No comments:

Post a Comment