繁体   English   中英

如何将字符串包含列表转换为python中的常规列表?

[英]how to convert string contains list to regular list in python?

我在 Apache Airflow 中有以下功能:

from airflow.utils.email import send_email
send_email(to=['a@mail.com','b@mail.com'],
           subject=....)

这很好用。

现在我不想对电子邮件列表进行硬编码,因此我将其保存为用户可以从其 UI 更改的可配置字段。 在此处输入图片说明

所以我将代码更改为:

NOTIFY_LIST = Variable.get("a_emails")
send_email([NOTIFY_LIST],
           subject=....)

但这不起作用。

当我做:

logging.info(NOTIFY_LIST)
logging.info([NOTIFY_LIST])
logging.info(NOTIFY_LIST.split(','))

我懂了:

'a@mail.com', 'b@mail.com'
[u"'a@mail.com', 'b@mail.com'"]
[u"'a@mail.com'", u" 'b@mail.com'"]

所以我的问题是:

['a@mail.com','b@mail.com']

[NOTIFY_LIST]

不一样。

我怎样才能解决这个问题? 我尝试了我能想到的任何转换。

尝试以下方法的建议;

logging.info(NOTIFY_LIST.replace("'", "").split(','))

这里的问题是列表中的元素包含引号。

如果字符串str.strip有一个 ' ,另一个答案将失败,以解决我将使用str.strip

logging.info([s.strip("' ") for s in NOTIFY_LIST.split(',')])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM