我正在True - False从文件中读取一个值,我需要将其转换为布尔值。当前,True即使将值设置为,它也始终将其转换为False。
这MWE是我正在尝试做的事情:
with open('file.dat', mode="r") as f:
for line in f:
reader = line.split()
# Convert to boolean <-- Not working?
flag = bool(reader[0])
if flag:
print 'flag == True'
else:
print 'flag == False'
该file.dat文件基本上由带有值True或False写入其中的单个字符串组成。这种安排看起来很复杂,因为这是来自更大代码的最小示例,这也是我将参数读入其中的方式。
为什么flag总是转换为True?