1

I'm trying to solve a problem where I have XML data that has 2 time series (sometimes more) of measurements in the same file. In this case temperature and wind speed measurements from the same time interval. See the XML file below:

   <?xml version="1.0" encoding="UTF-8"?>
<wfs:FeatureCollection
    timeStamp="2015-04-16T09:40:29Z"
    numberMatched="2"
    numberReturned="2"
           xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:om="http://www.opengis.net/om/2.0"
        xmlns:ompr="http://inspire.ec.europa.eu/schemas/ompr/2.0rc3"
        xmlns:omso="http://inspire.ec.europa.eu/schemas/omso/2.0rc3"
        xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:gmd="http://www.isotc211.org/2005/gmd"
        xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:swe="http://www.opengis.net/swe/2.0"
        xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0"
        xmlns:sam="http://www.opengis.net/sampling/2.0"
        xmlns:sams="http://www.opengis.net/samplingSpatial/2.0"
        xmlns:wml2="http://www.opengis.net/waterml/2.0"
    xmlns:target="http://xml.fmi.fi/namespace/om/atmosphericfeatures/0.95"
        xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd
        http://www.opengis.net/gmlcov/1.0 http://schemas.opengis.net/gmlcov/1.0/gmlcovAll.xsd
        http://www.opengis.net/sampling/2.0 http://schemas.opengis.net/sampling/2.0/samplingFeature.xsd
        http://www.opengis.net/samplingSpatial/2.0 http://schemas.opengis.net/samplingSpatial/2.0/spatialSamplingFeature.xsd
        http://www.opengis.net/swe/2.0 http://schemas.opengis.net/sweCommon/2.0/swe.xsd
        http://inspire.ec.europa.eu/schemas/ompr/2.0rc3 http://inspire.ec.europa.eu/draft-schemas/ompr/2.0rc3/Processes.xsd
        http://inspire.ec.europa.eu/schemas/omso/2.0rc3 http://inspire.ec.europa.eu/draft-schemas/omso/2.0rc3/SpecialisedObservations.xsd
        http://www.opengis.net/waterml/2.0 http://inspire.ec.europa.eu/draft-schemas/waterml/2.0rfc/waterml2.xsd
        http://xml.fmi.fi/namespace/om/atmosphericfeatures/0.95 http://xml.fmi.fi/schema/om/atmosphericfeatures/0.95/atmosphericfeatures.xsd">

        <wfs:member>
                <omso:PointTimeSeriesObservation gml:id="WFS-fip7WVVLMFTJudLNbf8qa6jEiVKJTowqYWbbpdOt.Lnl5dsPTTv3c3Trvlw9NGXk6ddNO3L2w7OuXhh08oWliy59O6pp25bX_0KFCMbHy51qRaFOO6dNGTVwzsu7JU07ctr_6FCiGdm26eenJzrMYsGLBg3Zmzfjw7MtambTfjSV3XpmcNbbh8RNPPph3Y8tK1dCA0tuHxF4b8ejnODHM3tuXpl30MPLDty9MvLnWt0y7eGXlh6deWVrbdPPnp3Z6mXx0rGnYZza3dds_NT6Yemnfu5zhNbj1w7NPTzJ3Zt9Y2_Nma3Pph6ad.6p54Za0N_DLuyYemFuc.m_llyceuXl5v6claYWbbpdOt.Lnl5dsPTTv3c3Trvlw9NGXk6ddNO3L2w7OuXhh08mh007ctPpl4T8hNDpp25bW_dlrGq1IYA-">

                    <om:phenomenonTime>
        <gml:TimePeriod  gml:id="time1-1-1">
          <gml:beginPosition>2015-01-01T00:00:00Z</gml:beginPosition>
          <gml:endPosition>2015-01-02T00:00:00Z</gml:endPosition>
        </gml:TimePeriod>
      </om:phenomenonTime>
      <om:resultTime>
        <gml:TimeInstant gml:id="time2-1-1">
          <gml:timePosition>2015-01-02T00:00:00Z</gml:timePosition>
        </gml:TimeInstant>
      </om:resultTime>      

        <om:procedure xlink:href="http://xml.fmi.fi/inspire/process/opendata"/>
                    <om:parameter>
                <om:NamedValue>
                    <om:name xlink:href="http://inspire.ec.europa.eu/codeList/ProcessParameterValue/value/groundObservation/observationIntent"/>
                    <om:value>
            atmosphere
                    </om:value>
                </om:NamedValue>
            </om:parameter>

                <om:observedProperty  xlink:href="http://data.fmi.fi/fmi-apikey/41156987-d187-45e2-a2d3-6113c39b15ab/meta?observableProperty=observation&amp;param=temperature&amp;language=eng"/>
                <om:featureOfInterest>
                    <sams:SF_SpatialSamplingFeature gml:id="fi-1-1-temperature">
          <sam:sampledFeature>
        <target:LocationCollection gml:id="sampled-target-1-1-temperature">
            <target:member>
            <target:Location gml:id="obsloc-fmisid-101007-pos-temperature">
                <gml:identifier codeSpace="http://xml.fmi.fi/namespace/stationcode/fmisid">101007</gml:identifier>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/name">Helsinki Rautatientori</gml:name>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/geoid">-16011680</gml:name>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/wmo">2934</gml:name>
            <target:representativePoint xlink:href="#point-101007"/>                                    


            <target:region codeSpace="http://xml.fmi.fi/namespace/location/region">Helsinki</target:region>

            </target:Location></target:member>
        </target:LocationCollection>
       </sam:sampledFeature>
                        <sams:shape>

                <gml:Point gml:id="point-1-1-temperature" srsName="http://www.opengis.net/def/crs/EPSG/0/4258" srsDimension="2">
                                <gml:name>Helsinki Rautatientori</gml:name>
                                <gml:pos>60.17169 24.94460 </gml:pos>
                            </gml:Point>

                        </sams:shape>
                    </sams:SF_SpatialSamplingFeature>
                </om:featureOfInterest>
          <om:result>
                    <wml2:MeasurementTimeseries gml:id="obs-obs-1-1-temperature">                         
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T00:00:00Z</wml2:time>
                      <wml2:value>4.6</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T01:00:00Z</wml2:time>
                      <wml2:value>4.5</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T02:00:00Z</wml2:time>
                      <wml2:value>4.2</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T03:00:00Z</wml2:time>
                      <wml2:value>4.0</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T04:00:00Z</wml2:time>
                      <wml2:value>4.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T05:00:00Z</wml2:time>
                      <wml2:value>4.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T06:00:00Z</wml2:time>
                      <wml2:value>4.5</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T07:00:00Z</wml2:time>
                      <wml2:value>4.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T08:00:00Z</wml2:time>
                      <wml2:value>3.3</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T09:00:00Z</wml2:time>
                      <wml2:value>2.9</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T10:00:00Z</wml2:time>
                      <wml2:value>2.5</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T11:00:00Z</wml2:time>
                      <wml2:value>2.4</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T12:00:00Z</wml2:time>
                      <wml2:value>2.5</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T13:00:00Z</wml2:time>
                      <wml2:value>2.7</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T14:00:00Z</wml2:time>
                      <wml2:value>2.8</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T15:00:00Z</wml2:time>
                      <wml2:value>3.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T16:00:00Z</wml2:time>
                      <wml2:value>3.3</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T17:00:00Z</wml2:time>
                      <wml2:value>3.7</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T18:00:00Z</wml2:time>
                      <wml2:value>4.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T19:00:00Z</wml2:time>
                      <wml2:value>4.1</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T20:00:00Z</wml2:time>
                      <wml2:value>4.4</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T21:00:00Z</wml2:time>
                      <wml2:value>4.7</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T22:00:00Z</wml2:time>
                      <wml2:value>4.6</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T23:00:00Z</wml2:time>
                      <wml2:value>4.7</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-02T00:00:00Z</wml2:time>
                      <wml2:value>4.2</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                         
                    </wml2:MeasurementTimeseries>
                </om:result>

        </omso:PointTimeSeriesObservation>
    </wfs:member>
        <wfs:member>
                <omso:PointTimeSeriesObservation gml:id="WFS-6fPzOlwmFuqLQd1YDR_0qMRemw2JTowqYWbbpdOt.Lnl5dsPTTv3c3Trvlw9NGXk6ddNO3L2w7OuXhh08oWliy59O6pp25bX_0KFCMbHy51qRaFOO6dNGTVwzsu7JU07ctr_6FCiGdm26eenJzrMYsGLBg3Zmzfjw7MtambTfjSV3XpmcNbbh8RNPPph3Y8tK1dCA0tuHxF4b8ejnODHM3tuXpl30MPLDty9MvLnWt307snPhly5NvNrbdPPnp3Z6mXx0rGnYZza3dds_NT6Yemnfu5zhNbj1w7NPTzJ3Zt9Y2_Nma3Pph6ad.6p54Za0N_DLuyYemFuc.m_llyceuXl5v6claYWbbpdOt.Lnl5dsPTTv3c3Trvlw9NGXk6ddNO3L2w7OuXhh08mh007ctPpl4T8hNDpp25bW_dlrGq1IYA-">


      <om:phenomenonTime xlink:href="#time1-1-1"/>
      <om:resultTime xlink:href="#time2-1-1"/>      

        <om:procedure xlink:href="http://xml.fmi.fi/inspire/process/opendata"/>
                    <om:parameter>
                <om:NamedValue>
                    <om:name xlink:href="http://inspire.ec.europa.eu/codeList/ProcessParameterValue/value/groundObservation/observationIntent"/>
                    <om:value>
            atmosphere
                    </om:value>
                </om:NamedValue>
            </om:parameter>

                <om:observedProperty  xlink:href="http://data.fmi.fi/fmi-apikey/41156987-d187-45e2-a2d3-6113c39b15ab/meta?observableProperty=observation&amp;param=windspeedms&amp;language=eng"/>
                <om:featureOfInterest>
                    <sams:SF_SpatialSamplingFeature gml:id="fi-1-1-windspeedms">
          <sam:sampledFeature>
        <target:LocationCollection gml:id="sampled-target-1-1-windspeedms">
            <target:member>
            <target:Location gml:id="obsloc-fmisid-101007-pos-windspeedms">
                <gml:identifier codeSpace="http://xml.fmi.fi/namespace/stationcode/fmisid">101007</gml:identifier>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/name">Helsinki Rautatientori</gml:name>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/geoid">-16011680</gml:name>
            <gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/wmo">2934</gml:name>
            <target:representativePoint xlink:href="#point-101007"/>                                    


            <target:region codeSpace="http://xml.fmi.fi/namespace/location/region">Helsinki</target:region>

            </target:Location></target:member>
        </target:LocationCollection>
       </sam:sampledFeature>
                        <sams:shape>

                <gml:Point gml:id="point-1-1-windspeedms" srsName="http://www.opengis.net/def/crs/EPSG/0/4258" srsDimension="2">
                                <gml:name>Helsinki Rautatientori</gml:name>
                                <gml:pos>60.17169 24.94460 </gml:pos>
                            </gml:Point>

                        </sams:shape>
                    </sams:SF_SpatialSamplingFeature>
                </om:featureOfInterest>
          <om:result>
                    <wml2:MeasurementTimeseries gml:id="obs-obs-1-1-windspeedms">                         
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T00:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T01:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T02:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T03:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T04:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T05:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T06:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T07:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T08:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T09:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T10:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T11:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T12:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T13:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T14:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T15:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T16:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T17:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T18:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T19:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T20:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T21:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T22:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-01T23:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                          
                        <wml2:point>
                            <wml2:MeasurementTVP> 
                                      <wml2:time>2015-01-02T00:00:00Z</wml2:time>
                      <wml2:value>NaN</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>                         
                    </wml2:MeasurementTimeseries>
                </om:result>

        </omso:PointTimeSeriesObservation>
    </wfs:member>
</wfs:FeatureCollection>

The data is nested within <wml2:MeasurementTimeseries gml:id="obs-obs-1-1-temperature"> for the temperature and within <wml2:MeasurementTimeseries gml:id="obs-obs-1-1-windspeedms"> for the wind speed (NaN's in this file).

I would like to iterate through those series separately and save them to .txt file in tabular format. I'm able to save the times series data, but I cant separate them column wise eg. to time|temp|wind speed format.

My current code works if the XML file has only one measurement (temp OR wind speed separately) because I only need to iterate through the file.

import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='XML_file.xml')
def parser(item1,item2):
    return item1.text,item2.text

data = [parser(item1,item2) for item1,item2 in zip(tree.iter(tag='{http://www.opengis.net/waterml/2.0}time'),tree.iter(tag='{http://www.opengis.net/waterml/2.0}value'))]



with open('data.txt', 'w') as fp:
        fp.write('\n'.join('%s|%s' % x for x in data))

Because the measurements for temp and ws are the same length how could I iterate through them (different tag perhaps?) and return tuple for each time point eg. (time,temp value, winds speed value)

EDIT: This prints results. But if I add tag like this i.iter(tag='{http://www.opengis.net/waterml/2.0}MeasurementTimeseries') I get TypeError: iter() takes no keyword arguments.

for i in tree.iter(tag='{http://www.opengis.net/waterml/2.0}MeasurementTimeseries'):
    for j in i.iter():
        print j.text

This prints:

2015-01-01T00:00:00Z
4.6




2015-01-01T01:00:00Z
4.5
4
  • I'm not sure I understand the problem, but it sounds like all you want to do is zip the time-and-temp data with the time-and-windspeed data, which you do with zip, which you're already using, so presumably you know how to use it again. Commented Apr 16, 2015 at 10:13
  • Or are you asking how to write the nested search (or single XPath query) to get the time and value from each particular series, instead of getting any time and value tags anywhere in the file? Commented Apr 16, 2015 at 10:14
  • Yeah i'm having trouble separating the two times series they use the same tag. My current code just runs through the file and saves the latter wind speed data below temperature data. I would like to use (maybe the XPath query?) search that I could use to "stack" the data column wise together to tuples of (time_i, temp_i, windspeed_i) for i=length(series). Commented Apr 16, 2015 at 10:29
  • XPath is probably a cleaner way to do this than the quick-and-dirty "iterate all two of the there-had-better-be-exactly-two-or-else series and zip them up" solution I wrote, so long as you understand XPath and how to use it with ElementTree; I just didn't want to write that solution without knowing whether you'd understand it. Commented Apr 16, 2015 at 10:40

1 Answer 1

1

I think what you're asking for is this: Instead of finding all time and value tags anywhere in the file, you want to find the time and value tags only under the temperature tag. (And then do the same thing for the time and value tags only under the windspeed tag, and then zip the two together, but you could do that part yourself if you had the first part.)

To do this, you need a nested search.

You can do that explicitly, with two searches—e.g., iterate over the two MeasurementTimeseries, then, within each one, iterate over its time and value descendants:

def parse_one_series(series):
    return [parser(item1,item2) for item1,item2 in
            zip(series.iter(tag='{http://www.opengis.net/waterml/2.0}time'),
                series.iter(tag='{http://www.opengis.net/waterml/2.0}value'))]

data = zip(*(parse_one_series(series) for series in
           tree.iter(tag='{http://www.opengis.net/waterml/2.0}MeasurementTimeseries')))

And now, each element in data is a pair of pairs, the first being time and temp, the second being time and windspeed. Although you might want to organize things a little more nicely:

data = [(time, temp, wind) for (time, temp), (_, wind) in data]
Sign up to request clarification or add additional context in comments.

9 Comments

You read my mind :) That's exactly what I wanted. Good to know you can seperate the series with nested search.
I tried to test your code but I got TypeError: zip() argument after * must be a sequence, not generator. Any ideas?
@ELEC: I tested this with Python 2.7 and 3.4, and didn't get that error with either one. (I also tried 2.6, but its older version of ElementTree doesn't have the iter method.) So… I'm not sure what your problem is. You can easily convert the generator into a list (which is a kind of sequence) by just using a list comprehension instead of a generator expression (that is, zip(*[…]) instead of zip(*(…)), but I'm not sure why you should need to.
@ELEC: In fact, I'm not sure how zip could even tell the difference. No matter what you put after the *, the function you call (zip) is going to receive the elements as separate arguments. So… this is baffling.
I changed to zip(*[]) and got another error from the function parse_one_series: TypeError: iter() takes no keyword arguments. Then I tried if the problem is the fact i use cElementTree and not the regular one. This produced another new error with the file saving part of the code: TypeError: not all arguments converted during string formatting
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.