Как написать с iterparse? - PullRequest
       27

Как написать с iterparse?

0 голосов
/ 25 июня 2018

Я пытаюсь пройтись по XML-документу, найти некоторые теги, объединить их в один новый и затем записать обратно в документ XML с помощью модуля ElementTree в python.

У меня есть код дляТочка, в которой я считаю, что это сработает, но когда я приступаю к части записи файла, я получаю сообщение об ошибке:

AttributeError: '_IterParseIterator' object has no attribute 'write'

файл, который я пытаюсь проанализировать, равен 120 МБ, поэтому я решил, что использование interparse будетболее эффективным.и это также то, с чем я более знаком.

 import xml.etree.ElementTree as ET #imports the ElementTree module for working with XML
import pprint
from collections import defaultdict


def is_tigerbase(elem):
    return (elem.tag =="tag") and (elem.attrib['k'] == "tiger:name_base")

def is_tigertype(elem):
    return (elem.tag =="tag") and (elem.attrib['k'] == "tiger:name_type")




def audit():
    tree = ET.iterparse('map')
    base = 0
    t_type = 0
    for event, elem in tree:
        #look for all nodes and ways 
        if elem.tag == "node" or elem.tag == "way":
            #loop through all the tags
            for tag in elem.iter('tag'):
                #if the tag is a tiger base then change the base value to 1
                #also get the v attribute and put it in the basedetail var
                #then stop the loop
                if is_tigerbase(tag):
                    base == 1
                    if 'v' in elem.attrib:
                        basedetail = elem.attrib['v']
                    break
            #loop through all the tags again
            for tag in elem.iter('tag'):
                #look for the tiger type tag, if there is one change the base
                #value to 1 and get the v attribute for the detail
                #stop the loop
                if is_tigertype(tag):
                    t_type == 1
                    if 'v' in elem.attrib:
                        t_typedetail = elem.attrib['v']
                    break
            #look to see if you had a base and a type and get ready to create
            #the new tag
            if base == 1 and t_type == 1:
                new = basedetail + " " + t_typedetail
                ET.SubElement(elem, "tag", k="addr:street", v=new)
                print(new)
            elif base == 1 and ttype == 0:
                new = basedetail
                ET.SubElement(elem, "tag", k="addr:street", v=new)
                print(new)
            base = 0
            ttype = 0

    tree.write('map')

audit()

Небольшой пример файла XML, который я анализирую:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.55.3 9da5e7ae">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2018-06-22T21:32:02Z"/>

  <bounds minlat="28.3156000" minlon="-81.6952000" maxlat="28.4497000" maxlon="-81.4257000"/>

  <node id="26794208" lat="28.3306444" lon="-81.5475040" version="14" timestamp="2014-07-07T10:17:59Z" changeset="24000940" uid="14293" user="KindredCoda"/>
  <node id="26794209" lat="28.3612495" lon="-81.5194078" version="17" timestamp="2014-07-05T01:17:25Z" changeset="23960255" uid="14293" user="KindredCoda"/>
  <node id="26794210" lat="28.3822849" lon="-81.5005573" version="25" timestamp="2018-02-26T21:48:01Z" changeset="56704055" uid="4018842" user="Stephen214">
    <tag k="highway" v="motorway_junction"/>
    <tag k="old_ref" v="27"/>
    <tag k="ref" v="68"/>
  </node>
  <way id="596852739" version="1" timestamp="2018-06-12T09:57:29Z" changeset="59771511" uid="5659851" user="marthaleena">
    <nd ref="5289076747"/>
    <nd ref="5126801577"/>
    <tag k="HFCS" v="Urban Collector"/>
    <tag k="highway" v="unclassified"/>
    <tag k="lanes" v="2"/>
    <tag k="name" v="Polynesian Isles Boulevard"/>
    <tag k="tiger:cfcc" v="A41"/>
    <tag k="tiger:county" v="Osceola, FL"/>
    <tag k="tiger:name_base" v="Polynesian Isles"/>
    <tag k="tiger:name_type" v="Blvd"/>
    <tag k="tiger:reviewed" v="no"/>
    <tag k="tiger:zip_left" v="34746"/>
    <tag k="tiger:zip_right" v="34746"/>
  </way>

1 Ответ

0 голосов
/ 25 июня 2018

Поскольку iterparse () не имеет функции записи, так как возвращает кортеж, вы не можете писать в документ так же, как и .parse ().переключение моего кода на использование синтаксического анализа решило проблему.

    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')
...