• <tfoot id='2AEhC'></tfoot><legend id='2AEhC'><style id='2AEhC'><dir id='2AEhC'><q id='2AEhC'></q></dir></style></legend>

  • <i id='2AEhC'><tr id='2AEhC'><dt id='2AEhC'><q id='2AEhC'><span id='2AEhC'><b id='2AEhC'><form id='2AEhC'><ins id='2AEhC'></ins><ul id='2AEhC'></ul><sub id='2AEhC'></sub></form><legend id='2AEhC'></legend><bdo id='2AEhC'><pre id='2AEhC'><center id='2AEhC'></center></pre></bdo></b><th id='2AEhC'></th></span></q></dt></tr></i><div id='2AEhC'><tfoot id='2AEhC'></tfoot><dl id='2AEhC'><fieldset id='2AEhC'></fieldset></dl></div>

    <small id='2AEhC'></small><noframes id='2AEhC'>

        • <bdo id='2AEhC'></bdo><ul id='2AEhC'></ul>

        将从文件读取的 True/False 值转换为布尔值

        Convert True/False value read from file to boolean(将从文件读取的 True/False 值转换为布尔值)
        <i id='Fo2jN'><tr id='Fo2jN'><dt id='Fo2jN'><q id='Fo2jN'><span id='Fo2jN'><b id='Fo2jN'><form id='Fo2jN'><ins id='Fo2jN'></ins><ul id='Fo2jN'></ul><sub id='Fo2jN'></sub></form><legend id='Fo2jN'></legend><bdo id='Fo2jN'><pre id='Fo2jN'><center id='Fo2jN'></center></pre></bdo></b><th id='Fo2jN'></th></span></q></dt></tr></i><div id='Fo2jN'><tfoot id='Fo2jN'></tfoot><dl id='Fo2jN'><fieldset id='Fo2jN'></fieldset></dl></div>
          <tbody id='Fo2jN'></tbody>
        <tfoot id='Fo2jN'></tfoot>
          <bdo id='Fo2jN'></bdo><ul id='Fo2jN'></ul>

              <legend id='Fo2jN'><style id='Fo2jN'><dir id='Fo2jN'><q id='Fo2jN'></q></dir></style></legend>

                <small id='Fo2jN'></small><noframes id='Fo2jN'>

                  本文介绍了将从文件读取的 True/False 值转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从文件中读取 True - False 值,我需要将其转换为布尔值.目前它总是将其转换为 True,即使该值设置为 False.

                  I'm reading a True - False value from a file and I need to convert it to boolean. Currently it always converts it to True even if the value is set to False.

                  这是我正在尝试做的 MWE:

                  Here's a MWE of what I'm trying to do:

                  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 文件基本上由一个字符串组成,其中写入值 TrueFalse.这种安排看起来非常复杂,因为这是来自更大代码的最小示例,这就是我将参数读入其中的方式.

                  The file.dat file basically consists of a single string with the value True or False written inside. The arrangement looks very convoluted because this is a minimal example from a much larger code and this is how I read parameters into it.

                  为什么flag总是转换成True?

                  推荐答案

                  bool('True')bool('False') 总是返回 True 因为字符串 'True' 和 'False' 不为空.

                  bool('True') and bool('False') always return True because strings 'True' and 'False' are not empty.

                  引用一位伟人(和 Python 文档):

                  To quote a great man (and Python documentation):

                  可以测试任何对象的真值,用于 if 或 while条件或作为以下布尔运算的操作数.这以下值被认为是错误的:

                  5.1. Truth Value Testing

                  Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

                  • 任何数字类型的零,例如,00L0.00j.
                  • 任何空序列,例如,''()[].

                  所有其他值都被认为是 true —所以很多类型的对象总是正确的.

                  All other values are considered true — so objects of many types are always true.

                  内置 bool函数使用标准的真值测试程序.这就是为什么你总是得到 True.

                  要将字符串转换为布尔值,您需要执行以下操作:

                  To convert a string to boolean you need to do something like this:

                  def str_to_bool(s):
                      if s == 'True':
                           return True
                      elif s == 'False':
                           return False
                      else:
                           raise ValueError # evil ValueError that doesn't tell you what the wrong value was
                  

                  这篇关于将从文件读取的 True/False 值转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <bdo id='nVxcj'></bdo><ul id='nVxcj'></ul>

                    <small id='nVxcj'></small><noframes id='nVxcj'>

                    1. <legend id='nVxcj'><style id='nVxcj'><dir id='nVxcj'><q id='nVxcj'></q></dir></style></legend>
                        <tbody id='nVxcj'></tbody>
                        <i id='nVxcj'><tr id='nVxcj'><dt id='nVxcj'><q id='nVxcj'><span id='nVxcj'><b id='nVxcj'><form id='nVxcj'><ins id='nVxcj'></ins><ul id='nVxcj'></ul><sub id='nVxcj'></sub></form><legend id='nVxcj'></legend><bdo id='nVxcj'><pre id='nVxcj'><center id='nVxcj'></center></pre></bdo></b><th id='nVxcj'></th></span></q></dt></tr></i><div id='nVxcj'><tfoot id='nVxcj'></tfoot><dl id='nVxcj'><fieldset id='nVxcj'></fieldset></dl></div>

                        <tfoot id='nVxcj'></tfoot>