从元组中删除信息

发布于 2025-01-19 15:25:04 字数 618 浏览 4 评论 0原文

我知道不能编辑Tupules,但我计划创建一个新的。我正在从文件中导入信息,然后删除名字,姓氏和出生年份。我只是不知道如何编辑它,以便我可以将所有内容删除以获取每个元素。对不起,如果这令人困惑!

这是我的代码!

#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打印

Mark,Acrobello,8/12/1988

,但每行的名称,姓氏和DOB的长度都不同。租赁帮助!谢谢!

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!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2025-01-26 15:25:04

我希望我正确理解了你..
您正在读取一个文件,在每一行中,都有一个员工,其数据格式如下:“Mark,Acrobello,8/12/1988”,并且您想要一个包含 3 个元素的元组:姓名、姓氏和日期。

您可以通过对字符串使用 split 方法来实现这

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

一点:

('Mark', 'Acrobello', '8/12/1988')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文