Python 我们如何打破这个值,得到错误-必须是str,而不是list

Python 我们如何打破这个值,得到错误-必须是str,而不是list,python,airflow,Python,Airflow,我是python新手,这里我想在逗号的基础上打破这个值。我试过用分割法,但不起作用 请参考下面的代码 report_name = task_instance.xcom_pull(task_ids='task_1', key='report_key') print('type report name : ',type(report_name)) ##INFO - type report name : <class 'str'> print('report name in email

我是python新手,这里我想在逗号的基础上打破这个值。我试过用分割法,但不起作用

请参考下面的代码

report_name = task_instance.xcom_pull(task_ids='task_1', key='report_key')
print('type report name : ',type(report_name))
##INFO - type report name :  <class 'str'>
print('report name in email op : ',report_name)        
##INFO - report name in email op :  
print('str report name : ',str(report_name))    
##INFO - str report name :  report_1,report_2,report_3
t_email_success = email_operator.EmailOperator(
                task_id='t_email_success',
                to=str(user_email),
                cc='abc@sample.com'
                subject='This is sample of subject',
                html_content="""Hello,<br><br>

                                Report is generated successfully <br><br>
                                File Names : """ + str(report_name).split(",") + """<br><br>



                                Thanks & Regards,<br><br>
                                """
            )
t_email_success.execute(context=kwargs) 
如有任何建议,将不胜感激


提前感谢。

如果您想要一个文件名列表,您可以像在代码第四行一样使用
report\u name

report\u name
是一个字符串,拆分它会将其转换为列表。你想达到什么目标

如果要在每个文件名后有新行,可以执行以下操作:

        html_content="""Hello,<br><br>

                        Report is generated successfully <br><br>
                        File Names : """ + report_name.replace(",","<br>") + """<br><br>



                        Thanks & Regards,<br><br>
                        """
html\u content=“”你好,

报告已成功生成

文件名:“+”报告名称。替换(“,”,“
”)+“

谢谢和问候,

"""

但是附加的文件名不能正确缩进,最好在
:)

之后添加一个

,如果您想要一个文件名列表,您可以像在代码的第四行那样使用
报告名

report\u name
是一个字符串,拆分它会将其转换为列表。你想达到什么目标

如果要在每个文件名后有新行,可以执行以下操作:

        html_content="""Hello,<br><br>

                        Report is generated successfully <br><br>
                        File Names : """ + report_name.replace(",","<br>") + """<br><br>



                        Thanks & Regards,<br><br>
                        """
html\u content=“”你好,

报告已成功生成

文件名:“+”报告名称。替换(“,”,“
”)+“

谢谢和问候,

"""

但是附加的文件名可能没有正确缩进,最好在
:)之后再添加一个

另一个答案是可以的,但我会稍微改变一下:

report_name = task_instance.xcom_pull(task_ids='task_1', key='report_key')
print('report name in email op : ',report_name)        
template="""Hello,<br><br>
Report is generated successfully <br><br>
File Names : {}<br><br>
Thanks & Regards,<br><br>
"""
t_email_success = email_operator.EmailOperator(
                task_id='t_email_success',
                to=user_email,
                cc='abc@sample.com',
                subject='This is sample of subject',
                html_content=template.format('<br>'.join(report_name.split(',')))
            )
t_email_success.execute(context=kwargs) 
report\u name=task\u instance.xcom\u pull(task\u id='task\u 1',key='report\u key')
打印('电子邮件操作中的报告名称',报告名称)
template=“”您好,

报告已成功生成

文件名:{}

谢谢和问候,

""" t_email_success=email_operator.EmailOperator( 任务\u id='t\u电子邮件\u成功', to=用户\电子邮件, 抄送abc@sample.com', subject='这是subject'的示例, html_content=template.format(“
”.join(报表名称.split(“,”)) ) t_email_success.execute(context=kwargs)
另一个答案是可以的,但我会稍微改变一下:

report_name = task_instance.xcom_pull(task_ids='task_1', key='report_key')
print('report name in email op : ',report_name)        
template="""Hello,<br><br>
Report is generated successfully <br><br>
File Names : {}<br><br>
Thanks & Regards,<br><br>
"""
t_email_success = email_operator.EmailOperator(
                task_id='t_email_success',
                to=user_email,
                cc='abc@sample.com',
                subject='This is sample of subject',
                html_content=template.format('<br>'.join(report_name.split(',')))
            )
t_email_success.execute(context=kwargs) 
report\u name=task\u instance.xcom\u pull(task\u id='task\u 1',key='report\u key')
打印('电子邮件操作中的报告名称',报告名称)
template=“”您好,

报告已成功生成

文件名:{}

谢谢和问候,

""" t_email_success=email_operator.EmailOperator( 任务\u id='t\u电子邮件\u成功', to=用户\电子邮件, 抄送abc@sample.com', subject='这是subject'的示例, html_content=template.format(“
”.join(报表名称.split(“,”)) ) t_email_success.execute(context=kwargs)
你能发布str(report\u name)
的样子吗?以及你的预期输出吗?
report\u name。split(“,”)
将在逗号上拆分
report\u name
。问题是您试图将结果列表添加到字符串中。不能将列表添加到字符串中。在
文件之后,您希望在邮件正文中有什么具体内容:
?Hello@SimonR,请检查预期输出并打印报表\u name Hello@Blotosmetek的声明,请检查预期输出您可以发布str(报表\u name)的外观以及您的预期输出吗?
报表\u name.split(“,”)
将在逗号上拆分
报告名称
。问题是您试图将结果列表添加到字符串中。不能将列表添加到字符串中。在邮件正文中的
文件:
之后,您到底想要什么?Hello@SimonR,请检查预期输出并打印报表\u name Hello@Blotosmetek,请检查预期输出