<p>我在python中查询MySQL数据库并选择一个布尔值——因此MySQL的响应要么是字符串'True'要么是'False'。我想根据MySQL的布尔值执行更多的代码。</p>
<p>例如</p>
<pre><code>data = 'False' # assume this is what was returned by MySQL.
if data:
print 'Success' #really this would be where I would execute other if True.
else:
print 'Fail' #really this would be where I would execute other code if False
</code></pre>
<p>但我不能这么做是因为</p>
<pre><code>if data:
</code></pre>
<p>永远都是真的</p>
<p>那么如何在python中将MySQL返回的字符串转换为布尔值呢?</p>
<p>目前我有:</p>
<pre><code>data = 'False' # assume this is what was returned by MySQL.
if data == 'True':
print 'Success'
else:
print 'Fail'
</code></pre>
<p>我相信在python中一定有更好的方法来实现这一点——很可能我遗漏了一些简单的东西。</p>