我要使用python内置的arr[0].pivot.id
来解析conf文件中的布尔值。
示例:
configparser
但是,当访问变量时,我注意到它被当作字符串处理。
[SECTION]
foo = False
此外,当我尝试更正此行为并将键>>> config['SECTION']['foo']
'False'
重新分配为其正确的布尔值代表时,会出现此错误
foo
不幸的是,这种行为导致以下情况意外执行的问题情况
>>> if config['SECTION']['foo'] == 'True':
... config['SECTION']['foo'] = True
... elif config['SECTION']['foo'] == 'False':
... config['SECTION']['foo'] = False
... else:
... Exception("foo must be bool")
TypeError: option values must be strings
用print(config['SECTION']['foo']) # 'False'
if config['SECTION']['foo']:
print('do things when foo is True') # this runs, but foo actually
# represents false, but in string form
解析时,如何用最少的开销处理布尔值?