有没有办法通过输入提示创建Python列表?

发布于 2024-12-24 22:16:31 字数 1809 浏览 1 评论 0原文

我正在创建一个 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]

[电子邮件受保护] 接收邮件。

我应该怎么办?

I'm creating a Python mailing List, but I had a problem at the end of function.

Problem is, the List must be like this:

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

My current code:

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

If you type:

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

An error comes up like this:

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

Else, If you type:

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

Only [email protected] receives the mail.

What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-12-31 22:16:31

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(',')]

The return of raw_input() is a string. You need to split it on the comma, then you'll get a list:

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

So in your example:

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

Another step can be done to remove any whitespace before/after each email:

mailinputs = raw_input('Enter all mails with comma: ')
receivers = [x.strip() for x in mailinputs.split(',')]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文