[英]using a for loop to read an external file in python and my else statement prints more than once, I need to find a way to change this
So for context I have two files: and external file called tasks.txt and my main file called task_manager.py.因此,对于上下文,我有两个文件:名为 tasks.txt 的外部文件和名为 task_manager.py 的主文件。 The format of tasks.txt is like this:
tasks.txt 的格式是这样的:
admin, Register Users with taskManager.py, Use taskManager.py to add the usernames and passwords for all team members that will be using this program., 10 Oct 2019, 20 Oct 2019, No admin,使用 taskManager.py 注册用户,使用 taskManager.py 为将使用该程序的所有团队成员添加用户名和密码。,2019 年 10 月 10 日,2019 年 10 月 20 日,否
Note that this is all one line.请注意,这都是一行。 To break this down it starts with the username and then separated by a comma and a space its the task title and the the task description and then the date assigned then the date due and finally whether the task is complete or not.
要分解它,它以用户名开头,然后用逗号和空格分隔,它是任务标题和任务描述,然后是分配的日期,然后是截止日期,最后是任务是否完成。
Now my job is to read this external file and print a set format of each line depending on if the username matches that of the ones in the file.现在我的工作是读取这个外部文件,并根据用户名是否与文件中的用户名匹配,打印每行的一组格式。 I have managed to do this first bit however when the username does not match I want to display a message saying "You have no tasks at the moment".
我已经设法做到了这一点,但是当用户名不匹配时,我想显示一条消息说“你现在没有任务”。 the problem is since it is a for loop it repeats this message for every line in the tasks.txt file.
问题是因为它是一个 for 循环,它会为 tasks.txt 文件中的每一行重复此消息。
This is the code I've written:这是我写的代码:
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()
There might be a simple answer to this but my head is scrambled lol.对此可能有一个简单的答案,但我的头很乱哈哈。
UPDATE:更新:
So I have changed my code to this所以我把我的代码改成了这个
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()
This solves my initial problem however this time the for loop doesn't work.这解决了我最初的问题,但是这次 for 循环不起作用。 Is it that once the external file is read once then it can't be read again?
是不是外部文件读了一次就不能再读了? Is there a solution to this?
有针对这个的解决方法吗?
Final update!最后更新!
Ive managed to solve this problem using.seek() to be able to reread the external file.我已经设法使用 .seek() 解决了这个问题,以便能够重新读取外部文件。 My final code is as follows:
我的最终代码如下:
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()
I see two solutions possible.我看到两种可能的解决方案。
First, you can read the file, put all the data in an array of map (dict in python) and go through this array.首先,可以读取文件,将所有数据放入一个map(python中的dict)数组,通过这个数组放入go。
Or, you can do like Tim said, and set a flag.或者,您可以像蒂姆说的那样,设置一个标志。
Here is code for both of those idea:这是这两个想法的代码:
Array of dict:字典数组:
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)
Flag:旗帜:
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()
I tried these two files with this tasks.txt and it worked:我用这个 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.