I'm learning Python and I'm trying to combine saving text specific element from the list to a txt file.
I managed to get the text out and it looks like this:
Project name: Main worker: Project name: Main worker:
and this is fine but I would like to add workers and projects that are in the lists to look like this:
Project name: Microsoft Main tester: Michael Scott Project name: Amazon Main tester: Dwight Schrute
And I'm not sure how to add that, this is what I got so far:
class Worker:
def __init__(self, name, lastname):
self.name = name
self.lastname = lastname
def name_lastname(self):
print(self.name, self.name)
class Project:
def __init__(self, name, worker):
self.name = name
self.worker = worker
workers = []
projects = []
name1 = input("Enter the name: ")
lastname1 = input("Enter the last name: ")
workers.append(name1 ' ' lastname1)
name2 = input("Enter the name: ")
lastname2 = input("Enter the last name: ")
workers.append(name2 ' ' lastname2)
projectname1 = input("Enter the name of the project: ")
projectname2 = input("Enter the name of the project: ")
projects.append(projectname1)
projects.append(projectname2)
print(workers)
print(projects)
file = open("projects.txt","w")
file.write("Project name: \n")
file.write("Main worker: \n")
file.write("Project name: \n")
file.write("Main worker: \n")
file.close()
CodePudding user response:
If I understood you correctly, just writing the names and projects like you did with the headers (e.g. "Project name:") would be sufficient. For example:
file.write("Project name: \n")
file.write(projects[0] "\n")
file.write("Main worker: \n")
file.write(workers[0] "\n")
file.write("Project name: \n")
file.write(projects[1] "\n")
file.write("Main worker: \n")
file.write(workers[1] "\n")
file.close()
For lists, you can access the first element with a list[0], the second one with list[1] and so on. This is however a fairly manual approach and you could use a loop here to iterate over the list entries if you have more entries later on.
CodePudding user response:
I not sure about your input and output data but I hope I understand it correct. And because you are a Python learner I took the liberty to introduce some other Python techniques to make the solution more Pythonic and less error prone.
The full example
#!/usr/bin/env python3
import pathlib
# example data (assuming both lists with same length)
all_workers = ['John Doe', 'Jenny Boe']
all_projects = ['Project Alpha', 'Project Beta']
# file path
projects_file_path = pathlib.Path('projects.txt')
# open the file
with projects_file_path.open('w') as file_handle:
# iterate over your lists side by side
for worker, project in all_workers, all_projects:
file_handle.write(f'Project name:\n{project}\n')
file_handle.write(f'Main worker:\n{worker}\n')
# let's look what is in the file
with projects_file_path.open('r') as file_handle:
print(file_handle.read())
The resulting projects.txt
file look like this. Is this what you wanted?
Project name:
Jenny Boe
Main worker:
John Doe
Project name:
Project Beta
Main worker:
Project Alpha
Step-by-step explained
I removed the two classes from your example code because I couldn't see an advantage of them. I also removed the input()
part and hard coded some sample data. Hope this is OK that way?
# example data (assuming both lists with same length)
all_workers = ['John Doe', 'Jenny Boe']
all_projects = ['Project Alpha', 'Project Beta']
To interact with files and the filesystem it is recommended to use Pythons pathlib
package. The with
block in the code make sure that the file is closed automatically when the block ends.
import pathlib
projects_file_path = pathlib.Path('projects.txt')
# open the file
with projects_file_path.open('w') as file_handle:
# write...
I think the for
-loop is self explaining. It's advantage is it prevents you from write repetitiv code.
I constructed the string written into the file with the f
-string like this.
file_handle.write(f'Project name:\n{project}\n')
But you could also use the old way with format()
.
file_handle.write('Project name:\n{}\n'.format(project))