编辑TXT文件的特定部分并保存更改

发布于 2025-01-21 09:55:03 字数 2459 浏览 0 评论 0原文

我正在尝试从TXT文件显示信息,然后保存所做的任何更改,然后将其写回TXT文件。

            with open("tasks.txt", 'r') as t_file:
                for line in t_file:
                    if line.startswith(username):
                        users_tasks_list.append(line)
                        for count, ele in enumerate(users_tasks_list, 1):
                            if task_select == int(count):
                                task_info = ele.split(",")
                                # I want to display the information in this format
                                print("\n" + " Task:\t\t" + task_info[1]
                                      + "\n" + "Assigned to:\t\t " + task_info[0]
                                      + "\n" + "Date assigned:\t" + task_info[3]
                                      + "\n" + "Due date:\t\t" + task_info[4]
                                      + "\n" + "Task complete?\t" + task_info[5]
                                      + "\n" + "Description:"
                                      + "\n" + task_info[2])

                                while True:
                                    # If the task is not completed entering 1 must change No to Yes
                                    # Entering 2 should allow you to edit the user of the task or the due date
                                    task_edit = int(input("\nEnter 1 to mark the task as complete \nEnter 2 to edit the task \nEnter 0 to return \n"))
                                    if task_edit == 1:
                                        task_info[5] = "Yes"
                                        t_file = open("tasks.txt", "w")
                                        user_tasks = " ".join(users_tasks_list)
                                        t_file.write(user_tasks)
                                        t_file.close()
                                        print("Task completed")
                                        

                                    elif task_edit == 2:
                                        print("success")
                                        if task_info[5] == "No":
                                            task_change = int(input("\nEnter 1 to edit the User \nEnter 2 to edit the due date of the task \n"))

这就是我所拥有的,我正在努力将信息保存回TXT文件。

TXT文件上的信息以下面的格式:

分配给(例如用户),任务标题,任务说明,分配的日期(格式:DD MMM yyyy),到期日期(格式:DD MMM YYYY),任务完成(例如NO)

每个任务都应在新线路上。

任何帮助将不胜感激。

I'm trying to display information from a txt file and then save whatever changes were made and write it back to the txt file.

            with open("tasks.txt", 'r') as t_file:
                for line in t_file:
                    if line.startswith(username):
                        users_tasks_list.append(line)
                        for count, ele in enumerate(users_tasks_list, 1):
                            if task_select == int(count):
                                task_info = ele.split(",")
                                # I want to display the information in this format
                                print("\n" + " Task:\t\t" + task_info[1]
                                      + "\n" + "Assigned to:\t\t " + task_info[0]
                                      + "\n" + "Date assigned:\t" + task_info[3]
                                      + "\n" + "Due date:\t\t" + task_info[4]
                                      + "\n" + "Task complete?\t" + task_info[5]
                                      + "\n" + "Description:"
                                      + "\n" + task_info[2])

                                while True:
                                    # If the task is not completed entering 1 must change No to Yes
                                    # Entering 2 should allow you to edit the user of the task or the due date
                                    task_edit = int(input("\nEnter 1 to mark the task as complete \nEnter 2 to edit the task \nEnter 0 to return \n"))
                                    if task_edit == 1:
                                        task_info[5] = "Yes"
                                        t_file = open("tasks.txt", "w")
                                        user_tasks = " ".join(users_tasks_list)
                                        t_file.write(user_tasks)
                                        t_file.close()
                                        print("Task completed")
                                        

                                    elif task_edit == 2:
                                        print("success")
                                        if task_info[5] == "No":
                                            task_change = int(input("\nEnter 1 to edit the User \nEnter 2 to edit the due date of the task \n"))

This is what I have, I'm struggling to save the information back to the txt file.

The information on the txt file is in the format below:

assigned to (e.g. user), task title, task description, date assigned (in the format: dd mmm yyyy), due date (in the format: dd mmm yyyy), task complete (e.g. No)

Each task should be on a new line.

Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文