Python Forum
[SOLVED] [BS] Why new tag only added at the end when defined outside the loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [BS] Why new tag only added at the end when defined outside the loop?
#1
Hello,

I need to add a new tag in all Placemark 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"):
	#works
    pm.coordinates.string="blah"

    #Works if re-create tag in the loop
    #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)

    #Shows things look good
    #print(pm.prettify())

print(soup.prettify())
print(pm.prettify()) within the loop shows things look like expected.

Any idea why?

Thank you.

Here's what I get after running the script:
Output:
<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>
---
Edit: The explanation is that a tag is unique, with references to the next and previous elements in the tree. So a fresh new tag is needed at every turn of the loop.
Reply
#2
(Sep-04-2022, 10:50 PM)Winfried Wrote: I notice it will only be added in the last Placemark when defined before entering the loop
It will only add stuff to last element in loop if you not insert to something that is defined before the loop.
from bs4 import BeautifulSoup

"""
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
    <Placemark>
    hello
    </Placemark>
    <Placemark>
    </Placemark>
 </Document>
</kml>
"""

soup = BeautifulSoup(open('pla.kml'), 'xml')
mark = soup.find_all('Placemark')
for item in mark:
    item.string = 'world'
>>> soup
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>world</Placemark>
<Placemark>world</Placemark>
</Document>
</kml>
Don't add [SOLVED] if is not that,do not add anything other that the Title is best.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 329 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Python beginner that needs an expression added to existing script markham 1 746 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,580 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,254 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 969 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,163 May-11-2022, 02:23 PM
Last Post: menator01
  Multiple user defined plots with secondary axes using for loop maltp 1 1,536 Apr-30-2022, 10:19 AM
Last Post: maltp
  httplib2 - how to see credentials added by add_credentials? MSV 2 2,201 Aug-05-2020, 12:24 PM
Last Post: MSV
  python library not defined in user defined function johnEmScott 2 3,937 May-30-2020, 04:14 AM
Last Post: DT2000
  How to show newly added column to csv johnson54937 3 2,258 Jan-07-2020, 04:01 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020