[英]AttributeError: 'str' object has no attribute 'str' - call the variable without ""
I have a string:我有一个字符串:
a = 'feature_selection = True'
When i want to call the variable "a" i get:当我想调用变量“a”时,我得到:
'feature_selection = True'
How can i call or store and call the variable "a" in order to get:我如何调用或存储并调用变量“a”以获得:
feature_selection = True
I have tried following, but got the error: "" all the time:我尝试了以下操作,但一直收到错误:“”:
a.str.split(',')
a.apply(lambda x: x[1:-1].split(','))
If you want to get out the value, you can split it by =
and take the last value.如果你想得到这个值,你可以用
=
分割它并取最后一个值。
>>> a = 'feature_selection = True'
>>> a
'feature_selection = True'
>>> val = a.split('=')[-1].strip()
>>> val
'True'
# Convert it to an actual boolean
>>> import ast
>>> ast.literal_eval(val)
True
If you want to execute the code, store by the string, you can use exec
:如果要执行代码,按字符串存储,可以使用
exec
:
>>> exec(a)
>>> feature_selection
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.