这是一个文本文件accountinfo.txt
:
Luke TonyHawk123! [email protected]
Cindy JasonVoorhees123! [email protected]
我想向用户询问他们的电子邮件,以便在左侧打印这些值(即用户名和密码)。例如,如果用户输入[email protected]
,则应返回Luke
和TonyHawk123
。
我尝试使用strip
和split
,但是我的逻辑是错误的。
到目前为止,我已经完成的工作:
account_file = open("accountinfo.txt")
email_string = account_file.read().strip().split()
while True:
email_account = input("Enter the email linked to your account: \n")
if email_account == "":
continue
if email_account in email_string:
# ???
else:
print("This email doesn't exist in our records.")
main()
您可以csv.reader
在这里申请:
import csv
with open("accountinfo.txt") as f:
reader = csv.reader(f, delimiter=" ")
email = input("Enter the email linked to your account: \n")
for row in reader:
if row and row[2] == email:
print(" ".join(row[:2]))
break
else:
print("This email doesn't exist in our records.")
main()
您还可以手动拆分每行:
with open("accountinfo.txt") as f:
email = input("Enter the email linked to your account: \n")
for line in f:
if email and email in line:
print(line.rsplit(" ", 1)[0])
break
else:
print("This email doesn't exist in our records.")
main()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句