我如何搜索和显示我的txt文件中的所有记录?



我以前尝试过一些代码,但没有成功。我的意思是,当我在一个文件中搜索一个单词时,它只显示它的第一个记录,然后转到后面的选项。它完全忽略了第二条记录。此外,当我搜索错误的客户名称时,它不会打印诸如"不存在"之类的语句。这是我的练习,可能是一个简单的问题,但我仍然是一个初学者。希望有人能帮我解决这个问题。谢谢,谢谢所有的帮助。

def search_payment():
print("Please Enter Customer Username For Searching")
search_username = input("Customer Username: ").upper()
show_search_payment = open("customer_order_records.txt", "r")
view_payment = show_search_payment.read()
view_payment_by_line = view_payment.splitlines()
show_search_payment.close()
for line in view_payment_by_line:
data = line.split(",")
if search_username in data[0]:
if search_username == data[0]:
print("Searching.....")
time.sleep(1)
print("Done Searching!nnn*----- "+ search_username + " CUSTOMERS ORDERS AND PAYMENT RECORDS -----*")
print("*" * 45)
print(" Usernamett: ", data[0],"n Datettt: ", data[1],"n Orderedtt: ", data[2],"n Total Quantity : ", data[3],"n Total Amountt: ", data[4],"n Payment Methodt: ", data[5])
print("*" * 45)
while True:
back = input("n(b) Back: ")
if back == "b" or back == "B":
search_order_payment()
else:
print("***Invalid Input! Please Try Againn")
search_payment()
else:
print("Customer Username Does Not Exist! Please Try Again!n")
while True:
try_again = input("(b) Back  (c) Continue : ")
if try_again == "b" or try_again == "B":
search_order_payment()
elif try_again == "c" or try_again == "C":
search_payment()
else:
print("***Invalid Input! Please Try Againn")
search_payment()

下面是我在txt文件中的内容:

ABC,Sun Jul 18 10:00:56 2021,NASI GORENG BIASA;RM6.00,1,RM6.00,CREDIT CARD,
WONG,Sun Jul 18 11:18:57 2021,ROTI CANAI KOSONG;RM1.60    TEH TARIK;RM2.60,3,RM5.80,TOUCH' N GO E-WALLET,
STANLEY,Mon Jul 19 11:05:18 2021,NASI GORENG SEAFOOD;RM10.50    NESCAFE NAI;RM2.80,2,RM13.30,CREDIT CARD,
WONG,Mon Jul 19 12:05:26 2021,ROTI CANAI PLANTA;RM3.90    TEH TARIK;RM2.60,3,RM10.40,TOUCH' N GO E-WALLET,
MAY,Mon Jul 19 12:20:51 2021,MURTABAK AYAM CHEESE;RM9.50    MILO;RM2.80,3,RM21.80,TOUCH' N GO E-WALLET,

对于您的问题,您应该使用for-else:
这是工作代码:

import time
def search_payment():
print("Please Enter Customer Username For Searching")
search_username = input("Customer Username: ").upper()
show_search_payment = open("customer_order_records.txt", "r")
view_payment = show_search_payment.read()
view_payment_by_line = view_payment.splitlines()
show_search_payment.close()
for line in view_payment_by_line:
data = line.split(",")
if search_username==data[0]:
print("Searching.....")
time.sleep(1)
print("Done Searching!nnn*----- "+ search_username + " CUSTOMERS ORDERS AND PAYMENT RECORDS -----*")
print("*" * 45)
print(" Usernamett: ", data[0],"n Datettt: ", data[1],"n Orderedtt: ", data[2],"n Total Quantity : ", data[3],"n Total Amountt: ", data[4],"n Payment Methodt: ", data[5])
print("*" * 45)
while True:
back = input("n(b) Back: ")
if back == "b" or back == "B":
search_order_payment()
else:
print("***Invalid Input! Please Try Againn")
search_payment()
else:
print("Customer Username Does Not Exist! Please Try Again!n")
while True:
try_again = input("(b) Back  (c) Continue : ")
if try_again == "b" or try_again == "B":
search_order_payment()
elif try_again == "c" or try_again == "C":
search_payment()
else:
print("***Invalid Input! Please Try Againn")
search_payment()
search_payment()

你的代码只是搜索列表的第一个元素,因为首先有if-else,这不会给程序检查列表的第二个元素。所以,你正面临着这个问题。

输出(使用2个现有值和1个随机值进行测试):

Please Enter Customer Username For Searching
Customer Username: ABC  <------------------------------ INPUT
Searching.....
Done Searching!

*----- ABC CUSTOMERS ORDERS AND PAYMENT RECORDS -----*
*********************************************
Username               :  ABC
Date                   :  Sun Jul 18 10:00:56 2021
Ordered                :  NASI GORENG BIASA;RM6.00
Total Quantity :  1
Total Amount   :  RM6.00
Payment Method :  CREDIT CARD
*********************************************
(b) Back: c
***Invalid Input! Please Try Again
Please Enter Customer Username For Searching
Customer Username: MAY  <---------------------------------- INPUT
Searching.....
Done Searching!

*----- MAY CUSTOMERS ORDERS AND PAYMENT RECORDS -----*
*********************************************
Username               :  MAY
Date                   :  Mon Jul 19 12:20:51 2021
Ordered                :  MURTABAK AYAM CHEESE;RM9.50    MILO;RM2.80
Total Quantity :  3
Total Amount   :  RM21.80
Payment Method :  TOUCH N GO E-WALLET
*********************************************
(b) Back: c
***Invalid Input! Please Try Again
Please Enter Customer Username For Searching
Customer Username: test  <----------------------------------- INPUT
Customer Username Does Not Exist! Please Try Again!
(b) Back  (c) Continue : c
Please Enter Customer Username For Searching
Customer Username:

相关内容

  • 没有找到相关文章

最新更新