Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing information from Tuples

I know tupules can’t be edited but I’m planning on creating a new one. I’m importing information from a file and then cutting out the first name, last name, and year of birth. I just don’t know how to edit it so I can cut out everything past the comma to get each element. Sorry if this is confusing!

Here’s my code!

#Open file for employee information
employee_info_file=open('employees.txt','r')

#Loop for all employees
for line in employee_info_file:
    #Read info and convert to tupule
    employee_info=employee_info_file.readline()
    employee_tupule=(employee_info)

    #Designate first name, last name, and year born
    first_name=
    last_name=
    year_born=

employee_tupule prints like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Mark,Acrobello,8/12/1988

But each line has a different length for first name, last name, and DOB. lease help! thanks!

>Solution :

I hope I understood you correctly..
you are reading a file and in each line, there is an employee with some data in this format: "Mark,Acrobello,8/12/1988" and you want to have a tuple that has 3 elements, name, last name, and the date.

you can achieve that by using the split method on the string:

employee_tuple = tuple("Mark,Acrobello,8/12/1988".split(","))
print(employee_tople)

prints:

('Mark', 'Acrobello', '8/12/1988')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading