首页 > 解决方案 > 在 python 中使用 exec() 运行具有字典语法的字符串

问题描述

txt_cmd = f"output_{oem_alias}_.rename(columns = {'code_tagic':'code_pred'},inplace=True)";exec(txt_cmd)  

尝试在 exec 中将字典作为字符串执行,我得到的错误如下图所示。

在此处输入图像描述

标签: pythondictionaryautomationexec

解决方案


txt_cmd = f"output_{oem_alias}_.rename(columns = {'{'}'code_tagic':'code_pred'{'}'},inplace=True)";exec(txt_cmd)

只需尝试使用{'{'}for {& {'}'} 来解决}这个问题,特别是在字典中使用。


推荐阅读