有人可以帮助我让我的程序根据哪个用户从 txt 文件上的信息登录到程序来打印用户特定信息。我的程序中有一个菜单,它仅通过键入"vm"来显示当前用户的任务。 这应该只显示分配给用户的任务。我能够向用户显示所有用户的所有任务,但是我如何仅从txt文件中提取用户特定信息。
txt 文件如下:
分配给任务的用户:
管理
任务标题 :
帮助
任务描述:
帮助奶奶
任务截止日期:
2020年01月24
日分配日期:
2020年01月24
日任务已完成:
不
...........................
分配给任务的用户:
约翰多
任务标题 :
走
任务描述:
遛狗
任务截止日期:
2020年01月26
日分配日期:
2020年01月24
日任务已完成:
不
如果我只想打印 johndoe 的任务,我将如何完成最后一行代码
到目前为止,我拥有的代码如下:
import datetime
users = {}
with open ('user.txt', 'rt')as username:
for line in username:
username, password = line.split(",")
users[username.strip()] = password.strip() # strip removes leading/trailing whitespaces
uinput = input("Please enter your username:n")
while uinput not in users:
print("Username incorrect.")
uinput = input("Please enter a valid username:n")
if uinput in users:
print ("Username correct")
with open('user.txt', 'rt') as password:
for line in password:
username, password = line.split(",")
users[password.strip()] = username.strip() # strip removes leading/trailing whitespaces
uinput2 = input("Please enter your password:n")
while uinput2 not in users:
print("Your username is correct but your password is incorrect.")
uinput2 = input("Please enter a valid password:n")
if uinput2 in users:
password2 = ("Password correct")
print (password2)
if password2 == ("Password correct"):
menu = (input("nPlease select one of the following options:nr - register userna - add tasknva - view all tasksnvm - view my tasksne - exitn"))
if menu == "r" or menu == "R":
new_user = (input("Please enter a new user name:n"))
new_password = (input("Please enter a new password:n"))
with open ('user.txt', 'a')as username:
username.write("n" + new_user + ", " + new_password)
elif menu == "a" or menu == "A":
task = input("Please enter the username of the person the task is assigned to.n")
while task not in username:
task = input("Username not registered. Please enter a valid username.n")
else:
task_title = input("Please enter the title of the task.n")
task_description = input("Please enter the task description.n")
task_due = input("Please input the due date of the task. (yyyy-mm-dd)n")
date = datetime.date.today()
task_completed = False
if task_completed == False:
task_completed = "No"
else:
task_completed = ("Yes")
with open('tasks.txt', 'a') as task:
task.write("nUser assigned to task:n" + uinput + "nTask Title :" + "n" + task_title + "n" + "Task Description:n" + task_description + "n" + "Task Due Date:n" + task_due + "n" + "Date Assigned:n" + str(date) + "n" + "Task Completed:n" + task_completed + "n")
elif menu == "va" or menu == "VA":
all_tasks = open('tasks.txt', 'r')
text = all_tasks.read()
all_tasks.close()
print(text)
elif menu == "vm" or menu == "VM"
任何帮助将不胜感激。 谢谢
这段代码使用正则表达式并从上面粘贴的文本文件中提取所需的字段。我将您发布的确切内容复制到文本文件中。我将该文件命名为 sample_text.txt
选项 1:这将提取分配给他们的所有用户名、任务和任务描述。
import re
with open('sample_text.txt','r') as fl:
lst = re.findall('.*n*(User assigned to task:).*n*(.*)n*(Task Title :)n*(.*)n*(Task Description:)n*(.*)n*', fl.read())
for item in lst:
print(f'"{item[1]}" is assigned with "{item[5]}"')
Output:
> "admin" is assigned to "Help Granny"
"johndoe" is assigned to "Walk the dog"
选项 2:这将询问您感兴趣的用户名,然后仅提取这些用户详细信息。
import re
name = input('Enter the name of the user:')
with open('sample_text.txt','r') as fl:
lst = re.findall('.*n*User assigned to task:.*n*(' + re.escape(name) + ')n*Task Title :n*(.*)n*Task Description:n*(.*)n*', fl.read())
for item in lst:
print(f'"{item[0]}" is assigned with task "{item[1]}" and task description "{item[2]}"')
Output:
> Enter the name of the user:johndoe
> "johndoe" is assigned with task "Walk" and task description "Walk the dog"