Q:

xpath - if else expression in Python

I have the following table structure:
In the gender column when a value exists, the gender is displayed between the  tag, but when the  tag does not exist it is not displayed and the value is a special character &nbsp;
<TABLE class="first">
   <TR>
      <TD></TD>
      <TD></TD>
      <TD></TD>
      <TD></TD>
   </TR>
   <TR VALIGN="top">
      <TD></TD>
      <TD><DIV>NAME</DIV></TD>
      <TD><DIV>AGE</DIV></TD>
      <TD><DIV>GENDER</DIV></TD>
   </TR>

   <TR VALIGN="top">
      <TD></TD>
      <TD><DIV>MARIA</DIV></TD>
      <TD><DIV>25</DIV></TD>
      <TD><DIV>F</DIV></TD>
   </TR>
    <TR VALIGN="top">
      <TD></TD>
      <TD><DIV>JOHN</DIV></TD>
      <TD><DIV>22</DIV></TD>
      <TD>&nbsp;</TD>
   </TR>
   <TR VALIGN="top">
      <TD></TD>
      <TD><DIV>PAUL</DIV></TD>
      <TD><DIV>36</DIV></TD>
      <TD>&nbsp;</TD>
   </TR>
   <TR VALIGN="top">
      <TD></TD>
      <TD><DIV>DEREK</DIV></TD>
      <TD><DIV>16</DIV></TD>
      <TD><DIV>M</DIV></TD>
   </TR>
</TABLE>

I'm doing the following:
for table in result.xpath('//table[@class="first"]'):     
    for i, tr in enumerate(table.xpath('//tr')):
        for j, td in enumerate(tr.xpath('td/div/|td')):
              if td.text == '&nbsp;':
                print '---'
              else:
                print td.text

How to print '---' if the character &nbsp exists in td.text?

A:

&nbsp; is an entity reference to the no-break space character (Unicode code point: U+00A0). To test if the text content of an element is equal to that character, you can use this:
if td.text == u'\u00A0':

Complete demonstration:
from lxml import html

table = html.parse("table.html")

for tr in table.xpath('//tr'):
    for td in tr.xpath('td/div|td'):
        if td.text == u'\u00A0':
            print 'BLANK VALUE'
        else:
            print td.text

Output:
None
None
None
None
None
None
NAME
None
AGE
None
GENDER
None
None
MARIA
None
25
None
F
None
None
JOHN
None
22
BLANK VALUE
None
None
PAUL
None
36
BLANK VALUE
None
None
DEREK
None
16
None
M

