0

I wish to split a long list of email address into lines, such as:

test01@testing.com; test02@testing.com; test03#testing1.com; test04@testing2.com,

I wish to split them into lines:

test01@testing.com (carriage return)
test02@testing.com (carriage return)
test03#testing1.com (carriage return)
test04@testing2.com

Can anyone pls help? Thanks.

4

4 回答 4

0

您可以使用split以分号分隔并strip删除任何空格:

>>> s = "test01@testing.com; test02@testing.com; test03#testing1.com"
>>> [(e.strip() + '\n') for e in s.split(';')]
['test01@testing.com\n', 'test02@testing.com\n', 'test03#testing1.com\n']
于 2012-04-07T13:35:39.650 回答
0

您可以使用split将地址拆分为多行,strip删除逗号和分号,并join重新排列它们。

>>> s = 'test01@testing.com; test02@testing.com; test03#testing1.com; test04@testing2.com,'
>>> print('\n'.join(m.strip(',;') for m in s.split())))
test01@testing.com
test02@testing.com
test03#testing1.com
test04@testing2.com
于 2012-04-07T13:36:44.113 回答
0

如何使用正则表达式:

import re
elist = re.findall(r'([^;,]+)', long_list)
print "".join("\n", elist)
于 2012-04-07T13:37:05.283 回答
0

如果您只想将它​​们维护为单个多行字符串,则简单的字符串替换就可以了:

long_list.replace('; ', '\n')

更灵活的解决方案可以使用正则表达式:

import re
re.sub(r'\s+;\s+', '\n')
于 2012-04-07T13:42:28.853 回答