在 python 中获取每个员工(即 'Mr T works at Ateam')的公司电子邮件列表和输出名片

take in a list of corporate emails and output contact cards for each employee (i.e. 'Mr T works at Ateam') in python

emails = ['mr.t@ateam.org',
          'baby.spice@spicegirls.com',
          'scary.spice@spicegirls.com',
            'kermit.frog@muppets.org']
a = []
for email in emails:
    a += email.replace('.',' ').split('@')

Print(a)

目前我能做的就这些了。我是新来的。有什么办法可以创建一个函数,在打印 'Mr T works at Ateam' 等输出时自动分隔姓名和电子邮件。谢谢

这里用到了以下函数:find从左到右查找第一个字符的索引,rfind从右到左查找,capitalize设置第一个大写字母。

索引切片用于提取字符串的所需部分。最后,所有部分都连接+。字符串在每次迭代时打印出来。还创建并打印了这些字符串的数组。 如果回答对您有帮助,那么您可以投上一票哦。

emails = ['mr.t@ateam.org',
          'baby.spice@spicegirls.com',
          'scary.spice@spicegirls.com',
          'kermit.frog@muppets.org']
a = []
work = "works at"
for email in emails:
    ind_p = email.find('.')
    ind_a = email.find('@')
    ind_sp = email.rfind('.')
    qqq = email[:ind_p].capitalize()+" "+email[ind_p+1:ind_a].capitalize()+" "+work+" "+email[ind_a+1:ind_sp].capitalize()
    print(qqq)
    a.append(qqq)

print(a)

输出

Mr T works at Ateam
Baby Spice works at Spicegirls
Scary Spice works at Spicegirls
Kermit Frog works at Muppets
['Mr T works at Ateam', 'Baby Spice works at Spicegirls', 'Scary Spice works at Spicegirls', 'Kermit Frog works at Muppets']