Skip to content
Snippets Groups Projects
Commit 2727cac3 authored by David Rouquet's avatar David Rouquet
Browse files

add snippet

parent 35447969
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
from lxml import etree
```
%% Cell type:code id: tags:
``` python
def unlize (text):
return ('I UNLized the following text : ###'+text+'###')
```
%% Cell type:code id: tags:
``` python
def nestedBody2Str (b) :
# Takes a node and return the children text nodes
# Nested texts are separated by commas
children = b.xpath('./node()')
result = ''
for child in children :
if type(child) == etree._ElementUnicodeResult :
result += str(child).strip()
else :
result += " "
nested = child.xpath('.//text()')
nestedStriped = [str(i).strip() for i in nested]
nestedFiltered = filter( lambda s: not (s == ''), nestedStriped)
result += ', '.join(nestedFiltered)
result += ". "
return (result)
```
%% Cell type:code id: tags:
``` python
filename = 'exemple_2007-ertms.xml'
doc = etree.parse(filename)
bodies = doc.xpath('//text_body')
for b in bodies:
#textList = b.xpath('.//')
parent = b.xpath('../node()')
unl_node = etree.Element('unl_body')
unl_node.text=unlize (nestedBody2Str(b))
parent.append(unl_node)
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment