我正在查看一個kml我試圖決議的檔案BeautifulSoup
我正在嘗試以下代碼,但未能實作所需的輸出:
from bs4 import BeautifulSoup
fn = r'sampleKMLFile.kml'
f = open(fn, 'r')
s = BeautifulSoup(f, 'xml')
pnodes = s.find_all('name')
z2 = s.find_all("Folder", {"name": 'MyNodes'})
基本上我想在熊貓資料框中實作以下目標:
MyNodes longitude latitude
Houston -95 33
Austin -97 33
KML 檔案很大,有以下我感興趣的內容。
<Folder>
<name>MyNodes</name>
<Placemark>
<name>Houston</name>
<Camera>
<longitude>-95</longitude>
<latitude>33</latitude>
<roll>-1.6</roll>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</Camera>
<styleUrl>#msn_placemark_circle</styleUrl>
<Point>
<coordinates>-95,33,0</coordinates>
<gx:drawOrder>1</gx:drawOrder>
</Point>
</Placemark>
<Placemark>
<name>Austin</name>
<Camera>
<longitude>-97</longitude>
<latitude>33</latitude>
<roll>-1.6</roll>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</Camera>
<styleUrl>#msn_placemark_circle</styleUrl>
<Point>
<coordinates>-97,33,0</coordinates>
<gx:drawOrder>1</gx:drawOrder>
</Point>
</Placemark>
</Folder>
編輯:由于上面的html格式字串在一個檔案中,我如何讀取檔案來生成sample或cnt從下面的兩個答案
uj5u.com熱心網友回復:
# cnt = """KML file content str"""
soup = BeautifulSoup(cnt, "lxml")
placemark = soup.find_all("placemark")
print("MyNodes longitude latitude")
for obj in placemark:
# here you can save the value inside i.e. a dictionary
print(obj.find("name").text, end=" ")
print(obj.find("longitude").text, end=" ")
print(obj.find("latitude").text)
# MyNodes longitude latitude
# Houston -95 33
# Austin -97 33
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
from bs4 import BeautifulSoup
import pandas as pd
sample = """<Folder>
<name>MyNodes</name>
<Placemark>
<name>Houston</name>
<Camera>
<longitude>-95</longitude>
<latitude>33</latitude>
<roll>-1.6</roll>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</Camera>
<styleUrl>#msn_placemark_circle</styleUrl>
<Point>
<coordinates>-95,33,0</coordinates>
<gx:drawOrder>1</gx:drawOrder>
</Point>
</Placemark>
<Placemark>
<name>Austin</name>
<Camera>
<longitude>-97</longitude>
<latitude>33</latitude>
<roll>-1.6</roll>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</Camera>
<styleUrl>#msn_placemark_circle</styleUrl>
<Point>
<coordinates>-97,33,0</coordinates>
<gx:drawOrder>1</gx:drawOrder>
</Point>
</Placemark>
</Folder>
"""
def finder(tag: str) -> list:
return [i.getText() for i in soup.find_all(tag) if i.getText() != "MyNodes"]
soup = BeautifulSoup(sample, features="xml")
df = pd.DataFrame(
zip(finder("name"), finder("longitude"), finder("latitude")),
columns=["Nodes", "Longitude", "Latitude"],
)
print(df)
輸出:
Nodes Longitude Latitude
0 Houston -95 33
1 Austin -97 33
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/440303.html
標籤:html xml 美丽的汤 公里 python-3.8
上一篇:將物件陣列中的缺失資料設定為零
