使用 Beautifulsoup,我需要读取 KML 文件,并在包含 LineString 部分的所有地标中插入一个新块。
这是 KML 文件:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Document.kml</name>
<Placemark>
<name>My track</name>
<LineString>
<coordinates>-0.376291,43.296237,199.75
-0.377381,43.29405</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
以下不起作用:
from bs4 import BeautifulSoup as Soup
with open('input.kml') as data:
kml_soup = Soup(data, 'lxml-xml') # Parse as XML
placemarks = kml_soup.find_all('Placemark')
for pm in placemarks:
if pm.find('LineString'):
print("LS found")
#How to insert new elements before LineString?
#<Style><LineStyle><width>3</width></LineStyle></Style>
style = kml_soup.new_tag("Style")
style.string = "<LineStyle><width>3</width></LineStyle>"
#AttributeError: 'NoneType' object has no attribute 'insert_before'
pm.string.insert_before(style)
蝴蝶不菲
相关分类