简体   繁体   English

python如何判断变量是否为布尔类型

[英]python how to judge whether a variable is boolean type

In python, how to judge whether a variable is bool type,python 3.6 using 在python中,如何判断变量是否为bool类型,python 3.6使用

    for i in range(len(data)):
        for k in data[i].keys():
            if type(data[i][k]) is types.BooleanType:
                data[i][k] = str(data[i][k])
            row.append(data[i][k])
            #row.append(str(data[i][k]).encode('utf-8'))
        writer.writerow(row)
        row = []

but it errors: 但它错误:

  if type(data[i][k]) is types.BooleanType:

  TypeError: 'str' object is not callable

you can check type properly with isinstance() 你可以用isinstance()正确检查类型

isinstance(data[i][k], bool)

will return true if data[i][k] is a bool 如果data[i][k]是bool,则返回true

 isinstance(data[i][k], bool) #returns True if boolean

代替 :

if type(data[i][k]) is types.BooleanType:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-如何判断输入变量是否在函数中重复? - Python - How to judge if input variable is duplicated in a function or not? 如何判断文件类型在python 2和python 3中都适用 - How to judge a file type both work in python 2 and python 3 如何判断给定坐标是否在某个城市? - How to judge whether or not a given coordinate is in a certain city? 检查变量的类型是否为Python中的特定类型 - Check whether the type of a variable is a specific type in Python 如何使用python正则表达式判断两个单词的意思是否相同 - How to judge whether the meaning of two words is the same using python regular expression 如何使用python判断键是否在字典列表的其他字典中(至少出现在2个不同的字典中) - How to judge whether a key is in the other dictionaries of a dictionary list(at least occurred in 2 different dictionaries) with python 在Python中如何将字符串类型的变量视为布尔值? - Howcome a string type variable is considered as boolean in Python? Python 判断每一行是否包含特定的词 - Python judge whether each line contains the specific words 如何检查变量是否为SwigObject类型 - How to check whether a variable is a SwigObject type 如何判断某个方法是否在父类中实现,如果可以,请使用它? - How to judge whether a method is implemented in parent class, and use it if so?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM