These characters are part of the XML document, so they are returned. You cannot escape them, but you can break them. You can call a method .strip()for each returned item:
results = [x.strip() for x in results]
. , .
, script:
from lxml import etree
with open('data.xml') as fd:
doc = etree.parse(fd)
results = doc.xpath(
"//table[@id='results']/tr[position()>1]/td/child::text()")
print 'Before stripping'
print repr(results)
print 'After stripping'
results = [x.strip() for x in results]
print repr(results)
:
<doc>
<table id="results">
<tr>
<th>ID</th><th>Name</th><th>Description</th>
</tr>
<tr>
<td>
1
</td>
<td>
Bob
</td>
<td>
A person
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Alice
</td>
<td>
Another person
</td>
</tr>
</table>
</doc>
:
Before stripping
['\n\t\t\t1\n\t\t\t', '\n\t\t\tBob\n\t\t\t', '\n\t\t\tA person\n\t\t\t', '\n\t\t\t2\n\t\t\t', '\n\t\t\tAlice\n\t\t\t', '\n\t\t\tAnother person\n\t\t\t']
After stripping
['1', 'Bob', 'A person', '2', 'Alice', 'Another person']