[weird] Why new tag only added at the end when defined outside the loop?

21 views
Skip to first unread message

Heck Lennon

unread,
Sep 4, 2022, 9:12:39 AM9/4/22
to beautifulsoup
Hello,

I'm learning BS, so it could be something I missed in the documentation.

I need to add a new tag in all Placemarks blocks in a KML file.

I notice it will only be added in the last Placemark when defined before entering the loop, while it works as expected if defined (needlessly) within the loop again and again:

===========================
from bs4 import BeautifulSoup

soup = BeautifulSoup(open("input.kml", 'r'), 'xml')

#Add element to all Placemarks
tag = soup.new_tag("i")
tag.string = "Don't"
for pm in soup.find_all("Placemark"):
    #tag = soup.new_tag("i")
    #tag.string = "Don't"
    #append/insert: only adds to last pm when tag defined outside loop!
    #pm.append(tag)
    pm.insert(0,tag)

    #print(pm.contents)

print(soup.prettify())
===========================

Any idea why?

Thank you.

Here's what I get after running the script:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>
   Document.kml
  </name>
  <Placemark>
   <name>
    Document Feature 1
   </name>
  </Placemark>
  <Placemark>
   <name>
    Document Feature 2
   </name>
  </Placemark>
  <Placemark>
   <i>
    Don't
   </i>
   <name>
    My track
   </name>
   <LineString>
    <coordinates>
     -0.376291,43.296237,199.75

        -0.377381,43.29405
    </coordinates>
   </LineString>  
  </Placemark>
 </Document>
</kml>

Heck Lennon

unread,
Sep 4, 2022, 10:42:44 AM9/4/22
to beautifulsoup
OTOH, modifying the text of each "coordinates" tag works as expected :-/

======
for pm in soup.find_all("Placemark"):
    pm.coordinates.string="blah"

print(soup.prettify())
======

Isaac Muse

unread,
Sep 4, 2022, 5:56:32 PM9/4/22
to beautifulsoup

while it works as expected if defined (needlessly) within the loop again and again

This is where your confusion is coming from. It is not needlessly done within the loop. If you want to insert a new tag for every Placemark, then you need a new tag for every insertion, not the same tag inserted in multiple locations.

When an HTML/XML object is created, every tag is unique as every tag has references to the next and previous element, so if you try to insert the same tag every time, the last place you insert it will be its new home as you will be updating its parent and sibling references each time, essentially moving it on every insertion.

Heck Lennon

unread,
Sep 4, 2022, 6:52:17 PM9/4/22
to beautifulsoup
Makes sense. Thanks much
Reply all
Reply to author
Forward
0 new messages