使用for循环在python中读取外部文件,并且我的else语句打印不止一次,我需要找到一种方法来改变这一点



所以对于上下文,我有两个文件:外部文件名为tasks.txt和我的主文件名为task_manager.py。tasks.txt的格式如下:

admin,注册用户与taskManager.py,使用taskManager.py添加用户名和密码为所有团队成员,将使用这个程序。, 2019年10月10日,2019年10月20日,否

注意这些都是一行。首先是用户名,然后用逗号和空格隔开,然后是任务标题和任务描述,然后是分配的日期,然后是截止日期,最后是任务是否完成。

现在我的工作是读取这个外部文件,并打印每行的一组格式,这取决于用户名是否与文件中的用户名匹配。我已经设法做到了这一点,但是当用户名不匹配时,我想显示一条消息说"你目前没有任务"。问题是,因为它是一个for循环,所以它会在tasks.txt文件中的每一行重复此消息。

这是我写的代码:
username = "admi"
f2 = open('tasks.txt', 'r')
for line in f2:
line3_split = line.split(', ')
if line3_split[0] == username:
user_format = f"""
Task:              {line3_split[1]}
Assigned to:       {line3_split[0]}
Date assigned:     {line3_split[3]}
Due date:          {line3_split[4]}
Task complete?     {line3_split[5]}
Task description:  
{line3_split[2]}
"""
print(user_format)
else:
print("You have no tasks at the moment.")

f2.close()

可能有一个简单的答案,但我的头是混乱的lol。

更新:

所以我把代码改成了

username = "admn"
f2 = open('tasks.txt', 'r')
is_user = f2.read()
if username in is_user:
for line in f2:
line3_split = line.split(', ')
if line3_split[0] == username:
user_format = f"""
Task:              {line3_split[1]}
Assigned to:       {line3_split[0]}
Date assigned:     {line3_split[3]}
Due date:          {line3_split[4]}
Task complete?     {line3_split[5]}
Task description:  
{line3_split[2]}
"""
print(user_format)
else:
print("You have no tasks at the moment.")
f2.close()

这解决了我最初的问题,但是这次for循环不起作用。是否一旦外部文件被读取一次,那么它就不能再读取了?有解决办法吗?

最后更新!

我设法解决了这个问题,使用.seek()能够重新读取外部文件。我的最终代码如下:

username = "admin"
f2 = open('tasks.txt', 'r')
is_user = f2.read()
if username in is_user:
f2.seek(0,0)
for line in f2:
line3_split = line.split(', ')
if line3_split[0] == username:
user_format = f"""
Task:              {line3_split[1]}
Assigned to:       {line3_split[0]}
Date assigned:     {line3_split[3]}
Due date:          {line3_split[4]}
Task complete?     {line3_split[5]}
Task description:  
{line3_split[2]}
"""
print(user_format)
else:
print("You have no tasks at the moment.")
f2.close()

我认为有两种可能的解决方案。

首先,您可以读取文件,将所有数据放入map (python中的dict)数组中,并遍历该数组。

或者,你可以像Tim说的那样,设置一个标志。

下面是这两个想法的代码:

数组:

username = "admi"
f2 = open('tasks.txt', 'r')
tasks = []
for line in f2:
line3_split = line.split(',')
this_task = {
"task": line3_split[1],
"assigned_to": line3_split[0],
"date_assigned": line3_split[3],
"due_date": line3_split[4],
"task_complete": line3_split[5],
"task_description": line3_split[2]
}
tasks.append(this_task)
f2.close()

if not any(d["assigned_to"] == username for d in tasks):
print("You have no tasks at the moment.")
else:
for task in tasks:
if task["assigned_to"] == username:
user_format = f"""
Task:              {task["task"]}
Assigned to:       {task["assigned_to"]}
Date assigned:     {task["date_assigned"]}
Due date:          {task["due_date"]}
Task complete?     {task["task_complete"]}
Task description:  
{task["task_description"]}
"""
print(user_format)

标记:

username = "admi"
f2 = open('tasks.txt', 'r')
no_task = True
for line in f2:
line3_split = line.split(',')
if line3_split[0] == username:
no_task = False
user_format = f"""
Task:              {line3_split[1]}
Assigned to:       {line3_split[0]}
Date assigned:     {line3_split[3]}
Due date:          {line3_split[4]}
Task complete?     {line3_split[5]}
Task description:  
{line3_split[2]}
"""
print(user_format)
if no_task:
print("You have no tasks at the moment.")

f2.close()

我用这个tasks.txt尝试了这两个文件,它工作了:

me,task1,description,20/20/20,21/21/21,True
admi,task3,description,20/20/20,21/21/21,False
albert,task2,description,20/20/20,21/21/21,False

相关内容

最新更新