Python中的Python:从孙子们那里获取文本

时间:2023-01-29 09:01:10

I'm quite novice in programming but I believe the data I'm looking for is quite easy to get, however I can't seem to wrap my head around it.

我在编程方面相当新手,但我相信我正在寻找的数据很容易获得,但我似乎无法绕过它。

My XML has several parents and each have of course their children with siblings and siblings with children. I am trying to reach a specific grandchild where one of its siblings has a specific word in a certain tag.

我的XML有几个父母,每个人当然都有孩子的兄弟姐妹和带孩子的兄弟姐妹。我试图达到一个特定的孙子,其中一个兄弟姐妹在某个标签中有一个特定的单词。

The XML (actually a KML) looks like this:

XML(实际上是KML)看起来像这样:

<Folder>
    <name> Run-1</name>
    <Placemark>
        <name> run 1</name>
        <Snippet></Snippet>
        <styleUrl>#flightline</styleUrl>
        <LineString>
            <extrude>0</extrude>
            <altitudeMode>clampToGround</altitudeMode>
            <coordinates>54.72664746,24.91070844,2008 54.76968330,24.91068150,2008
            </coordinates>
        </LineString>
    </Placemark>
</Folder>

Each folder named Run-X can have an infinite number of placemarks.

名为Run-X的每个文件夹都可以包含无限数量的地标。

I want the name of each folder and the coordinates in the (there is only one) placemark containing the the <styleUrl>#flightline</styleUrl> ONLY.

我想要每个文件夹的名称和仅包含 #flightline 的(只有一个)地标中的坐标。

That would build me a list of the run number and the 'flight line' coordinates.

这将为我建立一个运行编号和“飞行线”坐标的列表。

Of course I am trying the python and w3 schools tutorials and I understand the basics but I can't seem to put it all together. Do I need a for loop to reach each child and a nested loop to reach every sub-child? Or can I just look for tags throughout the tree and get the coordinates value IF there is a <styleUrl>#flightline</styleUrl> tag?

当然我正在尝试python和w3学校的教程,我理解基础知识,但我似乎无法将它们放在一起。我需要一个for循环来覆盖每个孩子,还有一个嵌套循环来覆盖每个子孩子?或者我可以只在整个树中查找标签并获取坐标值,如果有 #flightline 标签?

I have been playing around with root.iter and root.findall but I can't seem to get any kind of result.

我一直在玩root.iter和root.findall,但我似乎无法得到任何结果。

2 个解决方案

#1


0  

How about following? Assuming your kml data resides in data.xml

跟着怎么样?假设您的kml数据驻留在data.xml中

from collections import OrderedDict
from xml.etree import ElementTree as ET

tree = ET.parse("data.xml")
root = tree.getroot()

result = OrderedDict()
for folder in root.iter('Folder'):
    for placemark in folder.findall('Placemark'):
        if placemark.find('styleUrl').text == '#flightline':
            result[folder.find('name').text.strip()] = placemark.find('LineString/coordinates').text.strip()

print(result)

#2


0  

Thanks so much for your help. I found a solution based on to your code:

非常感谢你的帮助。我找到了一个基于你的代码的解决方案:

for folder in root.iter('Folder'):
for placemark in folder.findall('Placemark'):
    if placemark.find('styleUrl').text == '#flightline':
        runLine = folder.find('name').text[5:]
        startLat = placemark.find('LineString/coordinates').text[:11]
        startLong = placemark.find('LineString/coordinates').text[12:23]
        endLat = placemark.find('LineString/coordinates').text[29:40]
        endLong = placemark.find('LineString/coordinates').text[41:52]
        print ('Flightline: ' + runLine + ', coordinates start: ' + startLat + ' ' + startLong + '. Coordinates end: ' + endLat + ' ' + endLong + '.')

In case you are wondering, I'm trying to read files outputted by an aerial survey program (flightlines are lines flown to take pictures) and create a csv and flight plan file for the GPS in the aircraft to read so it can fly them automatically.

如果您想知道,我正在尝试读取航空测量程序输出的文件(航线是飞行拍摄的照片)并为飞机上的GPS创建一个csv和飞行计划文件,以便它可以自动飞行。

Now I need to find a way to remove the <kml> </kml> tags from the intial .kml file (on whatever line they might be) and only then open and parse it, output the line number and coordinates (with custom name) according to the flightline in a CSV and also output another flightplan file in a Garmin specific format. At least now I know how to scan the file. Thanks again Sir!

现在我需要找到一种方法从初始.kml文件中移除 标签(在它们可能的任何行上),然后打开并解析它,输出行号和坐标(使用自定义名称) )根据CSV中的航线,并以Garmin特定格式输出另一个flightplan文件。至少现在我知道如何扫描文件。再次感谢先生!

#1


0  

How about following? Assuming your kml data resides in data.xml

跟着怎么样?假设您的kml数据驻留在data.xml中

from collections import OrderedDict
from xml.etree import ElementTree as ET

tree = ET.parse("data.xml")
root = tree.getroot()

result = OrderedDict()
for folder in root.iter('Folder'):
    for placemark in folder.findall('Placemark'):
        if placemark.find('styleUrl').text == '#flightline':
            result[folder.find('name').text.strip()] = placemark.find('LineString/coordinates').text.strip()

print(result)

#2


0  

Thanks so much for your help. I found a solution based on to your code:

非常感谢你的帮助。我找到了一个基于你的代码的解决方案:

for folder in root.iter('Folder'):
for placemark in folder.findall('Placemark'):
    if placemark.find('styleUrl').text == '#flightline':
        runLine = folder.find('name').text[5:]
        startLat = placemark.find('LineString/coordinates').text[:11]
        startLong = placemark.find('LineString/coordinates').text[12:23]
        endLat = placemark.find('LineString/coordinates').text[29:40]
        endLong = placemark.find('LineString/coordinates').text[41:52]
        print ('Flightline: ' + runLine + ', coordinates start: ' + startLat + ' ' + startLong + '. Coordinates end: ' + endLat + ' ' + endLong + '.')

In case you are wondering, I'm trying to read files outputted by an aerial survey program (flightlines are lines flown to take pictures) and create a csv and flight plan file for the GPS in the aircraft to read so it can fly them automatically.

如果您想知道,我正在尝试读取航空测量程序输出的文件(航线是飞行拍摄的照片)并为飞机上的GPS创建一个csv和飞行计划文件,以便它可以自动飞行。

Now I need to find a way to remove the <kml> </kml> tags from the intial .kml file (on whatever line they might be) and only then open and parse it, output the line number and coordinates (with custom name) according to the flightline in a CSV and also output another flightplan file in a Garmin specific format. At least now I know how to scan the file. Thanks again Sir!

现在我需要找到一种方法从初始.kml文件中移除 标签(在它们可能的任何行上),然后打开并解析它,输出行号和坐标(使用自定义名称) )根据CSV中的航线,并以Garmin特定格式输出另一个flightplan文件。至少现在我知道如何扫描文件。再次感谢先生!