473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

elementtree behavior

I have an XML snippet that I parse with ElementTree, and I get an element
called self.makeToc

Now this code:

print self.makeToc
if self.makeToc != None:
print 'here'
if self.makeToc: print 'but not here'

gives this result:
<Element toc at 40197e88>
here

Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
That is, if self.makeToc isn't None, why don't I get past 'if self.makeToc'

Can someone point out what am I not understanding?

thanks,
--Tim

Jul 18 '05 #1
2 1182
"Tim Arnold" <ti****@sas.com> wrote in message
news:cj**********@license1.unx.sas.com...
I have an XML snippet that I parse with ElementTree, and I get an element
called self.makeToc

Now this code:

print self.makeToc
if self.makeToc != None:
print 'here'
if self.makeToc: print 'but not here'

gives this result:
<Element toc at 40197e88>
here

Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
That is, if self.makeToc isn't None, why don't I get past 'if self.makeToc'
Can someone point out what am I not understanding?

thanks,
--Tim

Not sure about your XML Element classes, but a number of Python objects will
evaluate to false even if not set to None:
- boolean with value of 'false'
- integer with value of 0
- list with no elements

Perhaps your Element class evaluates to false if it has no child nodes, or
some similar thing.

-- Paul
Jul 18 '05 #2
Tim Arnold wrote:
I have an XML snippet that I parse with ElementTree, and I get an element
called self.makeToc

Now this code:

print self.makeToc
if self.makeToc != None:
print 'here'
if self.makeToc: print 'but not here'

gives this result:
<Element toc at 40197e88>
here

Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
That is, if self.makeToc isn't None, why don't I get past 'if self.makeToc'


because it's a sequence without any items.

this is discussed in the element overview:

http://effbot.org/zone/element.htm

Note that in ElementTree 1.2 and earlier, the sequence behaviour
means that an element without subelements tests as false (since it's
an empty sequence). To check the return value from a function or
method that may return None instead of a node, you must use an
explicit test.

node = fetchnode()

if not node: # careful!
print "node not found, or node has no subnodes"

if node is None:
print "node not found"

</F>

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stewart Midwinter | last post by:
I want to parse a file with ElementTree. My file has the following format: <!-- file population.xml --> <?xml version='1.0' encoding='utf-8'?> <population> <person><name="joe" sex="male"...
1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
9
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
2
by: Sébastien Boisgérault | last post by:
Hi all, The unicode code points in the 0000-001F range -- except newline, tab, carriage return -- are not legal XML 1.0 characters. Attempts to serialize and deserialize such strings with...
5
by: saif.shakeel | last post by:
#!/usr/bin/env python from elementtree import ElementTree as Element tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.