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

相关问题