如何在Python中从文本文件中提取特定数据?

在框架中

这是一个文本文件accountinfo.txt

Luke TonyHawk123! [email protected]  
Cindy JasonVoorhees123! [email protected]

我想向用户询问他们的电子邮件,以便在左侧打印这些值(即用户名和密码)。例如,如果用户输入[email protected],则应返回LukeTonyHawk123
我尝试使用stripsplit,但是我的逻辑是错误的。

到目前为止,我已经完成的工作:

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()
奥尔文·罗格特(Olvin Roght)

您可以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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Python从文本文件中提取特定数据并写入CSV

从文本文件中提取特定数据

解析文本文件中的特定数据

如何从python中的文本文件中提取特定内容?

如何从文本文件中提取数据到python中的二维数组

如何在gnuplot中绘制文本文件中的某些特定数据

如何从文本文件中获取特定数据

如何使用findall函数从python中的文本文件中提取特定的url

如何从文本文件中提取特定的值/字段?

从文本文件中提取行的特定PIECE(Python)

如何从C#中的文本文件中提取特定文本

从文本文件中提取数据

Python从文本文件中提取特定数字

如何从文本文件中提取数据?

使用python从多个文本文件中提取数据

我想从文本文件中提取特定数据

从文本文件中提取数据(python)

从python 3中的文本文件中提取数据

使用 python 从文本文件中提取特定行

如何在文本文件中的某个字符串之后从python中的文本文件中提取数据?

如何在文本文件中提取不同的数据子集并将每个子集传递到另一个文本文件中?

在 Python 中匹配和从文本文件中提取

如何从文本文件中提取特定值?

如何读取这个结构奇特的文本文件中的特定数据?Python

使用python从文本文件中提取数据

从 Python 中的文本文件中提取句子

如何从 Makefile 中的文本文件中提取值?

如何从文本文件中提取特定的目标编号

使用python从文本文件中的特定模式中提取文本