2012-04-07 50 views

回答

0

可以使用split多行分裂地址,strip以除去逗號和分號,和join重新排列它們。

>>> s = '[email protected]; [email protected]; test03#testing1.com; [email protected],' 
>>> print('\n'.join(m.strip(',;') for m in s.split()))) 
[email protected] 
[email protected] 
test03#testing1.com 
[email protected] 
+0

謝謝你這麼多。我學會了。 – user1319102 2012-04-07 14:00:36

+0

查看鏈接,文檔解釋了許多重要的功能。順便說一句,如果這個(或任何其他)答案解決了你的問題,請隨時[accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)(並可能upvote)它。謝謝! – phihag 2012-04-07 14:21:23

+1

你必須小心。 ;,可以是電子郵件地址的本地部分(@之前的所有內容)的一部分。所以簡單的分裂並不是防彈的。 – esskar 2012-04-25 10:01:31

0

如何使用正則表達式:

import re 
elist = re.findall(r'([^;,]+)', long_list) 
print "".join("\n", elist) 
+0

嗯,正則表達式不考慮空格和逗號。此外,還有一個語法錯誤 - 你錯過了一個'''。另外,'str.join'除了'self'外只有一個參數。最重要的是,print不再是Python 3中的語句。 – phihag 2012-04-07 13:40:01

+0

白色空間是無關緊要的。海報想要捕捉地址。至於錯字,這是固定的。 – user590028 2012-04-07 21:06:01

0

如果你只是想維護他們作爲一個單一的多行字符串,一個簡單的字符串替換將這樣的伎倆:

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

更靈活的解決方案可以使用正則表達式:

import re 
re.sub(r'\s+;\s+', '\n')