8

In python, how to judge whether a variable is bool type,python 3.6 using

    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
3

2 Answers 2

32

you can check type properly with isinstance()

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

will return true if data[i][k] is a bool

Sign up to request clarification or add additional context in comments.

Comments

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

instead of :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.