提问人:tekkyprograms 提问时间:12/28/2022 最后编辑:tekkyprograms 更新时间:12/28/2022 访问量:196
在 python 中使用 for 循环读取外部文件,而我的 else 语句打印了不止一次,我需要找到一种方法来改变这一点
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
问:
因此,对于上下文,我有两个文件:外部文件称为 tasks.txt我的主文件称为 task_manager.py。任务 .txt 的格式如下:
admin, 向 taskManager.py 注册用户, 使用 taskManager.py 为将使用此程序的所有团队成员添加用户名和密码., 10 10 1019, 20 10月 2019, 否
请注意,这都是一行。为了分解这一点,它从用户名开始,然后用逗号和空格分隔,它是任务标题和任务描述,然后是分配的日期,然后是截止日期,最后是任务是否完成。
现在我的工作是读取这个外部文件,并根据用户名是否与文件中的用户名匹配,打印每行的固定格式。我已经设法做到了这一点,但是当用户名不匹配时,我想显示一条消息,说“您目前没有任务”。问题是因为它是一个 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()
这个问题可能有一个简单的答案,但我的头被打乱了,哈哈。
更新:
所以我把我的代码改成了这个
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()
答:
0赞
Pierre Marsaa
12/28/2022
#1
我认为有两种可能的解决方案。
首先,你可以读取文件,将所有数据放在一个 map 数组中(python 中的 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)
旗:
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()
我用这个任务.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
评论
found = False
if not Found: