python从字符串变量创建变量

时间:2018-05-23 15:35:41

标签: python string variables

我怎样才能得到以下代码?我想为变量Reputation Risk_22may_only赋值。我从here获得了以下代码。但它对我不起作用

risk_type="Reputation Risk"

 xxx1=risk_type + "_22may_only"
exec(xxx1 + " = 'something else'")

exec(xxx1 + " = 'something else'")

Traceback (most recent call last):

  File "C:\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-38-c9a2a2cf48c3>", line 1, in <module>
    exec(xxx1 + " = 'something else'")

  File "<string>", line 1
    Reputation Risk_22may_only = 'something else'
                                  ^
SyntaxError: invalid syntax

0 个答案:

没有答案