Search code examples
tuplesstripsplice

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

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')