'How do you use a for loop to read a file and print each line of a file in a certain format
I need the below text file to print like below:
admin, Register Users with taskManager.py, Use taskManager.py to add usernames and passwords for all team members that will be using this program,10 Oct 2019, 20 Oct 2019, No admin, Assign initial tasks , Use taskManager.py to assign each team member with appropriate tasks, 10 Oct 2019, 25 Oct 2019, No peter, data storage, Store data into a secure database and run monthly reports, 25 Jun 2022, 2022-03-10, No
Needs to print each line in the file in this format out in this format:
Task: Assigned initial tasks Assigned to: admin Date assigned: 10 Oct 2019 Due date: 25 Oct 2019 Task Complete? No Task description: Use taskManager.py to assign member
This is the code that I wrote but I cant seem to figure out run the loop correctly:
with open("tasks.txt", "r") as f:
list_string = f.read().split(", ")
print("______________________________")
for line in list_string:
print(f"Task:\t\t\t {line}\nAssigned to:\t\t\t {line}\nDate assigned:\t\t\t {line}\nDue date:\t\t\t {line}\nTask complete:\t\t\t {line}\nTask description:\n{line}")
print("____________________________")
Solution 1:[1]
The general mechanism for reading line oriented data would be:
with open('<your input file>') as infile:
for line in infile:
pass # process each line here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert Winestein |
