【问题标题】:searching through text files then printing the data搜索文本文件然后打印数据
【发布时间】:2019-09-26 14:51:23
【问题描述】:

我正在制作一个巴士预订系统,但我不知道如何搜索文本文件然后打印数据。

这是它应该如何工作的:

用户输入姓名和旅行日期

程序搜索文本文件以找到与旅行名称和日期匹配的行

如果程序找到了,它会打印票数、姓名、出发城市、目的地城市、旅行日期、旅行时间

这是我到目前为止的全部代码

restart = 'Y'

while restart != ('y'):
    passenger = open("reservation.txt" , "a+")

    print("\n1. Make Reservation")

    print("2. Modify Reservation")

    print("3. Cancel Reservation")

    print("4. Search Reservation")

    print("5. Exit")

    option = int(input("\nEnter your option: "))


    if option == 1: //makes reservation
        passenger.write("\n")
        tickets = input("\nNumber of Tickets: ")
        space = len(tickets)+1
        passenger.write(tickets.ljust(space))

        name = input("\nPassenger Name: ")
        space = len(name)+1
        passenger.writelines(name.ljust(space))

        departure  = input("\nDeparture City: ")
        space = len(departure)+1
        passenger.writelines(departure.ljust(space))

        destination  = input("\nDestination City: ")
        space = len(destination)+1
        passenger.writelines(destination.ljust(space))

        date = input("\nDate of Travel: ")
        space = len(date)+1
        passenger.writelines(date.ljust(space))

        time = input("\nTime of Travel: ")
        space = len(time)+1
        passenger.writelines(departure.ljust(space))

        print("Your reservation has been submitted. \nThank you!")
        restart = input("Try again? (y/n): ").upper()
        passenger.close()
        if restart == ('N'):
            exit(0)

    elif option == 2:
        //edits reservation

    elif option == 3:
        //cancels reservation

    elif option == 4: //searches through the reservations
        passenger = open("reservation.txt" , "r+")
        n = input("Enter passenger name: ")
            #dt = input("Date of Travel: ")
        if n in passenger.read():
            x = 0
            for i in range(1, x, 1):   
                contents = passenger.readlines(i)
                print(contents)
        else:
            print('doesnt exist')

        restart = input("Try again? (y/n): ").upper()
        passenger.close()
        if restart == ('N'):
            exit(0)  

    elif option == 5:
        exit(0)

    else:
        print('invalid input')

【问题讨论】:

  • 欢迎来到 SO。请使用tour 并花时间阅读How to Ask 以及该页面上的其他链接。你有问题吗?你的解决方案有效吗?有没有不足?如果是这样,它是如何不足的?你怀疑它的任何部分吗?为什么?你有没有给我们minimal reproducible example?

标签: python python-3.x for-loop search input


【解决方案1】:
with open("reservation.txt") as file:
    lines = file.readlines()
    for line in lines:
        if name in line and date in line: # name and date are the variables you're looking for
            print(line)
            break

您创建一个行列表,然后使用 in 关键字搜索该列表中的每一行。

>>> 'python' in 'hello world this is python'
True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多