我的输入是
email_list =
"mail1@mail.com,mail2@mail.com, mail3@mail.com mail4@mail.com
mail5@mail.com"
例外输出
['mail1@mail.com', 'mail2@mail.com', 'mail3@mail.com', 'mail4@mail.com', 'mail5@mail.com']
我试过这个方法,但它只会拆分(,)分开。请告诉我。
output = email_list.split(',')
提前致谢...
答案 0 :(得分:5)
您可以使用正则表达式:
>>> import re
>>> email_list = "mail1@mail.com,mail2@mail.com, mail3@mail.com mail4@mail.com"
>>> re.split(r'[, ]*',email_list)
['mail1@mail.com', 'mail2@mail.com', 'mail3@mail.com', 'mail4@mail.com']
re.split
根据r'[, ]*'
拆分字符串,该字符串与逗号(,
)和空格()的任意组合分开。
答案 1 :(得分:1)
没有正则表达式,
>>> email_list = "mail1@mail.com,mail2@mail.com, mail3@mail.com mail4@mail.com"
>>> [i for x in email_list.split(',') if x for i in x.split(' ') if i]
['mail1@mail.com', 'mail2@mail.com', 'mail3@mail.com', 'mail4@mail.com']
答案 2 :(得分:1)
>>> email_list = "mail1@mail.com,mail2@mail.com, mail3@mail.com mail4@mail.com"
>>> email_list.replace(',', ' ').split()
['mail1@mail.com', 'mail2@mail.com', 'mail3@mail.com', 'mail4@mail.com']
答案 3 :(得分:0)
也没有正则表达式:)
import re
email_list = "mail1@mail.com,mail2@mail.com, mail3@mail.com mail4@mail.com"
print filter(lambda x:x, sum([x.split(",") for x in email_list.split(" ")], []))
答案 4 :(得分:-1)
尝试
email_list.replace(" ","")
使用之前
output = email_list.split(',')