user9973598
user9973598

Reputation:

Parsing XML with Python: Keeping text within attribute while deleting tag around it

Input:
<p>
<milestone n="14" unit="verse" />
 The name of the third river is
<placeName key="tgn,1130850" authname="tgn,1130850">Hiddekel</placeName>: this is the one which flows in front of Assyria. The fourth
river is the <placeName key="tgn,1123842" authname="tgn,1123842">Euphrates</placeName>. 
</p>

Desired Output:

<p>
<milestone n="14" unit="verse" />
 The name of the third river is Hiddekel: this is the one which flows in front of Assyria. The fourth river is the Euphrates. 
</p>

Hi there, I would like to figure a way by which to extract text from a sub element (placeName) and put it back into the larger body of text. I have similar issues elsewhere in the XML file, such as for names of people. I would like to be able to extract names and places without getting rid of milestones. Thank you for your help!

Current code:

for p in chapter.findall('p'):
    i = 1
    for text in p.itertext():
        file.write(body.attrib["n"] + " " + chapter.attrib["n"] + ":" +  str(i) + text)
        i = i + 1

Upvotes: 1

Views: 113

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24930

It can be done with beautifulsoup and the unwrap() method:

from bs4 import BeautifulSoup as bs

snippet = """your html above"""

soup = bs(snippet,'lxml')
pl = soup.find_all('placename')
for p in pl:
    p.unwrap()
soup

Output:

<html><body><p>
<milestone n="14" unit="verse"></milestone>
 The name of the third river is
Hiddekel: this is the one which flows in front of Assyria. The fourth
river is the Euphrates. 
</p>
</body></html>

Upvotes: 0

Related Questions