2017-01-12 8 views
0

In Python ist, wie zu beurteilen, ob eine Variable bool-Typ ist, 3.6 PythonPython wie zu beurteilen, ob eine Variable boolean Typ

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 = [] 

verwenden, aber es Fehler:

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

    TypeError: 'str' object is not callable 
+0

>>> a = False >>> isinstance (a, bool) Wahre >>> isinstance (a, str) Falsch – CSJ

+0

Mögliches Duplikat von [Ermitteln Sie den Typ eines Python-Objekts] (http://stackoverflow.com/questions/2225038/determine-the-type-of-a-python-object) –

+1

Mögliches Duplikat von [Wie wird der Typ von Python verglichen? ein Objekt in Python?] (http://stackoverflow.com/questions/707674/how-to-compare-type-of-an-object-in-python) –

Antwort

5

Sie können überprüfen, geben Sie richtig mit isinstance()

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

kehrt true wenn data[i][k] ist ein Bool

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

statt:

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