如何在Python中从文本文件中提取特定数据?
这是一个文本文件如何在Python中从文本文件中提取特定数据?,python,python-3.x,split,Python,Python 3.x,Split,这是一个文本文件accountinfo.txt: Luke TonyHawk123! luke33@gmail.com Cindy JasonVoorhees123! cindy5@yahoo.com 我想让用户的电子邮件打印左侧的值(即用户名和密码)。例如,如果用户输入luke33@gmail.com,它应该返回Luke和TonyHawk123 我试过使用strip和split,但我的逻辑是错误的 到目前为止我所做的工作: account_file = open("accountinfo
accountinfo.txt
:
Luke TonyHawk123! luke33@gmail.com
Cindy JasonVoorhees123! cindy5@yahoo.com
我想让用户的电子邮件打印左侧的值(即用户名和密码)。例如,如果用户输入luke33@gmail.com
,它应该返回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()
你可以在这里申请:
也可以手动拆分各行:
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()
我不知道每封电子邮件的信息是如何排列的,但它们是否在同一个文件中。我做了一个类似的程序,但是每个用户名的信息都用一个类似于这样的标记分隔开来:“--------------------”。因此,我重复了几行,直到它找到了电子邮件。一旦它找到它,我会打印每一行,直到它找到另一行“--------------------”。所以用户的所有信息都在这些行之间。根据信息的排列方式,您可以执行类似的操作。您可以使用以下命令遍历文件的行:
validation = False
for line in file:
if email in line:
validation = True
while line != "---------------":
print(line, end="")
if validation == False:
print("This email doesn't exist in our records.")
试试这个:
accounts = {}
with open("accountinfo.txt", "r") as account_file:
email_string = account_file.readlines()
for n in email_string:
x = str(n.split(" ")[2]).split("\n")
if len(x) == 2:
accounts[x[0]] = n.split(" ")
accounts[x[0]][2] = x[0]
else:
accounts[n.split(" ")[2]] = n.split(" ")
while True:
email_account = input("Enter the email linked to your account: \n")
if email_account == "":
continue
if email_account in accounts[email_account][2]:
for i in range(2):
print(accounts[email_account][i])
else:
print("This email doesn't exist in our records.")
尝试将
与open(file.foo)
一起使用,因为如果脚本崩溃,那么文件将被关闭,而不像open(file.foo)
检查电子邮件字符串中的email\u帐户不起作用,因为(“Luke”,“TonyHawk123!”)luke33@gmail.com“”
是电子邮件字符串中的内容。您需要提取电子邮件地址,并可能将其放入字典中进行检索。因此,您建议在每行上迭代9次,以形成dict
,其中电子邮件是一个键?它会迭代每行,因此在本例中仅迭代2次,如果我理解这个问题,而他没有说任何关于效率的话,那么只需计算一下您调用str.split()
的次数,它至少在字符串上迭代一次。我不知道这也是在字符串上迭代。我是一个初学者,我已经做了半年了,我自学的越来越多。但对我来说,我认为这是解决其他人垃圾代码初学者问题的实践,但它仍然有效。您认为它如何在不迭代和检查每个字符分隔符的情况下分割字符串?您应该在发布的代码上多做一些工作,因为在当前版本中,您过度使用了str.split()
,创建了无用的dict
,并在没有任何exit的情况下启动了external循环。python中有专门为此目的而设计的语句。
accounts = {}
with open("accountinfo.txt", "r") as account_file:
email_string = account_file.readlines()
for n in email_string:
x = str(n.split(" ")[2]).split("\n")
if len(x) == 2:
accounts[x[0]] = n.split(" ")
accounts[x[0]][2] = x[0]
else:
accounts[n.split(" ")[2]] = n.split(" ")
while True:
email_account = input("Enter the email linked to your account: \n")
if email_account == "":
continue
if email_account in accounts[email_account][2]:
for i in range(2):
print(accounts[email_account][i])
else:
print("This email doesn't exist in our records.")