E 'sicuro per controllare se una variabile myvar
ha valore non-None semplicemente:Verificare se il nodo XML ElementTree è Nessuno/Falso
if myvar:
print('Not None detected')
che sto chiedendo questo perché ho una variabile ed è stato verificare se la variabile non era None
semplicemente if variable:
ma il controllo non ha funzionato. La variabile contiene alcuni dati ma stava valutando su False nel controllo if
.
codice completo:
from xml.etree import ElementTree as ElementTree
root = ElementTree.fromstring('Some xml string')
parameters = root.find('Some Tag')
udh = parameters.find('UDH')
if udh and udh.text: # In this line the check is failing, though the udh variable has value: <Element 'UDH' at 0x7ff614337208>
udh = udh.text
# Other code
else:
print('No UDH!') # Getting this output
Questo è in realtà sbagliato. '' se myvar: '' assumendo '' myvar'' è '' None'' in realtà non valuterà '' True'' e quindi il tuo campione non funzionerà. È comunque consigliabile utilizzare il modulo: '' se myvar non è None: '' or '' se myvar è None: '' –
Qui è stata data una risposta: http://stackoverflow.com/questions/3965104/not-none -test-in-python – ka2m
Il tuo controllo fallisce perché non c'è testo in udh? Potrebbe essere un nodo XML vuoto. – Midnighter