簡體   English   中英

使用 for 循環讀取 python 中的外部文件並且我的 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 的主文件。 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()

對此可能有一個簡單的答案,但我的頭很亂哈哈。

更新:

所以我把我的代碼改成了這個

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)數組,通過這個數組放入go。

或者,您可以像蒂姆說的那樣,設置一個標志。

這是這兩個想法的代碼:

字典數組:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM