2012-01-04 142 views
0

我正在創建一個Python郵件列表,但我在函數結束時遇到了問題。有沒有辦法通過輸入提示製作Python列表?

問題是,該列表必須是這樣的:

['[email protected]', '[email protected]', '[email protected]'] 

我當前的代碼:

mailinputs = raw_input('Enter all mails with comma: ') 
receivers = [mailinputs] 

如果鍵入:

'[email protected]', '[email protected]', '[email protected]' 

一個錯誤出現這樣的:

Probe failed: Illegal envelope To: address (invalid domain name): 

否則,如果鍵入:

[email protected], [email protected], [email protected] 

只有[email protected]接收郵件。

我該怎麼辦?

+0

你的意思是一個列表,而不是字典。 – 2012-01-05 00:04:32

+0

是的,我很抱歉。 – 2012-01-05 00:05:42

回答

7

返回raw_input()是一個字符串。你需要將它拆分的逗號,那麼你會得到一個列表:

>>> '[email protected],[email protected],[email protected]'.split(',') 
['[email protected]', '[email protected]', '[email protected]'] 

所以在你的例子:

mailinputs = raw_input('Enter all mails with comma: ') 
receivers = mailinputs.split(',') 

另一個步驟可以完成之前刪除任何空白/每封電子郵件後:

mailinputs = raw_input('Enter all mails with comma: ') 
receivers = [x.strip() for x in mailinputs.split(',')] 
+0

謝謝..這是一個愚蠢的問題。 – 2012-01-05 00:09:47

相關問題