本文介绍了接受2个XML元素并合并为1个新元素-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用OpenStreetMap XML文档进行项目.该项目的一部分是验证某些数据及其一致性.我对使用Python和XML文件非常陌生,所以我真的不知道从哪里开始.

I am currently working on a project using OpenStreetMap XML documents. Part of the project is to verify some of the data and it's consistency. I am fairly new with Python and with working with XML files so I have really no idea where to start.

这是我的XML文档的摘录:

Here is a snippet of my XML document:

  <way id="11005330" version="2" timestamp="2013-02-05T20:56:45Z" changeset="14926577" uid="451693" user="bot-mode">
<nd ref="98006629"/>
<nd ref="98006630"/>
<nd ref="98006631"/>
<tag k="highway" v="residential"/>
<tag k="name" v="Kiwi Court"/>
<tag k="tiger:cfcc" v="A41"/>
<tag k="tiger:county" v="Lake, FL"/>
<tag k="tiger:name_base" v="Kiwi"/>
<tag k="tiger:name_type" v="Ct"/>
<tag k="tiger:reviewed" v="no"/>
<tag k="tiger:zip_left" v="34714"/>
<tag k="tiger:zip_right" v="34714"/>

我现在要做的是:

    <tag k="tiger:name_base" v="Kiwi"/>
    <tag k="tiger:name_type" v="Ct"/>

并将它们组合成一个新标签:

and combine them into one new tag:

<tag k="addr:street" v="Kiwi Ct"/>

另一件事是,并非所有这些都同时具有name_base和name_type.因此对于那些我只想创建addr:street标签的人.

the other thing is that not all of these have both the name_base and name_type. so for those I just want to create the addr:street tag.

这是一个非常大的文件,因此必须仔细检查每个文件并创建它.创建新标签后,我将需要继续删除该元素.

This is an extremely large file so it would have to look through each one and create it. After it creates the new tag I will then need to go ahead and remove the element.

我正在使用:import xml.etree.cElementTree as ET

编辑

我能够解决部分问题

    root = tree.getroot()
    for way in root.findall(".//way"):
        kbool = False
        tbool = False
        for key in way.iterfind(".//tag"):
            if key.attrib['k'] == "tiger:name_base":
                kbool = True
                # print(key.attrib['v'])
                base = key.attrib['v']
            if key.attrib['k'] == "tiger:name_type":
                tbool = True
                ttype = key.attrib['v']
        if kbool == True and tbool == True:
            ET.SubElement(way, 'tag k="addr:street" v="{} {}"'.format(base, ttype))
        elif kbool == True and tbool == False:
            ET.SubElement(way, 'tag k="addr:street" v="{}"'.format(base))


tree.write('maps')

我现在遇到的问题是,即使对于没有Tiger:name_base密钥的方式,它也在写地址属性.

The issue I'm having now is that it is writing the address attribut even for ways that do not have the tiger:name_base key.

推荐答案

使用ElementTree.

XML = """<root>
    <way id="11005330" version="2" timestamp="2013-02-05T20:56:45Z" changeset="14926577" uid="451693" user="bot-mode">
        <nd ref="98006629"/>
        <nd ref="98006630"/>
        <nd ref="98006631"/>
        <tag k="highway" v="residential"/>
        <tag k="name" v="Kiwi Court"/>
        <tag k="tiger:cfcc" v="A41"/>
        <tag k="tiger:county" v="Lake, FL"/>
        <tag k="tiger:name_base" v="Kiwi"/>
        <tag k="tiger:name_type" v="Ct"/>
        <tag k="tiger:reviewed" v="no"/>
        <tag k="tiger:zip_left" v="34714"/>
        <tag k="tiger:zip_right" v="34714"/>
    </way>
</root>"""

演示:

import xml.etree.ElementTree as ET

tree = ET.parse(filename)
doc = tree.getroot()
for way in doc.findall(".//way"):            #Find all way tags
    name_base = way.find('.//tag[@k="tiger:name_base"]').get("v")     #Get tiger:name_base attr
    way.remove(way.find('.//tag[@k="tiger:name_base"]'))              #Remove Tag
    name_type = way.find('.//tag[@k="tiger:name_type"]').get("v")     #Get tiger:name_type attr
    way.remove(way.find('.//tag[@k="tiger:name_type"]'))              #Remove Tag
    newNode = ET.SubElement(way, '''tag k="addr:street" v="{} {}"'''.format(name_base, name_type))    #Add New Tag
tree.write(r"C:\Users\Rakesh\Desktop\testFiles\A2.xml")               #Write to file

输出:

<root>
    <way changeset="14926577" id="11005330" timestamp="2013-02-05T20:56:45Z" uid="451693" user="bot-mode" version="2">
        <nd ref="98006629" />
        <nd ref="98006630" />
        <nd ref="98006631" />
        <tag k="highway" v="residential" />
        <tag k="name" v="Kiwi Court" />
        <tag k="tiger:cfcc" v="A41" />
        <tag k="tiger:county" v="Lake, FL" />
        <tag k="tiger:reviewed" v="no" />
        <tag k="tiger:zip_left" v="34714" />
        <tag k="tiger:zip_right" v="34714" />
    <tag k="addr:street" v="Kiwi Ct" /></way>
</root>

这篇关于接受2个XML元素并合并为1个新元素-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:57