Как извлечь образец большого XML файла с python? - PullRequest
0 голосов
/ 23 апреля 2020

У меня очень большое дерево XML, и я хотел бы взять его пример для проверки своего кода, который я хочу использовать для извлечения информации по всему дереву.

Данные выглядят так:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v6.dtd">

<population desc="Switzerland Baseline">

    <attributes>
        <attribute name="coordinateReferenceSystem" class="java.lang.String" >Atlantis</attribute>
    </attributes>


<!-- ====================================================================== -->

    <person id="1117137">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >24</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
            <attribute name="carAvailability" class="java.lang.String" >some</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588212</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1136390100003</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >true</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >false</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.4622222222222222" selected="no">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="07:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="07:04:10" trav_time="00:05:28">
                <route type="links" start_link="408171" end_link="126690" trav_time="00:05:28" distance="3356.007319888538" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690</route>
            </leg>
            <activity type="work" link="126690" facility="86174" x="647377.2" y="6860623.5" start_time="07:34:10" end_time="16:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="16:04:10" trav_time="00:05:20">
                <route type="links" start_link="126690" end_link="408171" trav_time="00:05:20" distance="3015.427007883928" vehicleRefId="null">126690 126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:34:10" end_time="18:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:34:10" trav_time="00:08:28">
                <route type="generic" start_link="408171" end_link="251314" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="shop" link="251314" facility="77137" x="650113.2" y="6860046.8" start_time="18:44:10" end_time="19:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:34:10" trav_time="00:08:28">
                <route type="generic" start_link="251314" end_link="408171" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:44:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

        <plan score="-0.46277777777777773" selected="yes">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="07:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="07:04:10" trav_time="00:05:28">
                <route type="links" start_link="408171" end_link="126690" trav_time="00:05:28" distance="3356.007319888538" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690</route>
            </leg>
            <activity type="work" link="126690" facility="86174" x="647377.2" y="6860623.5" start_time="07:34:10" end_time="16:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="16:04:10" trav_time="00:05:20">
                <route type="links" start_link="126690" end_link="408171" trav_time="00:05:20" distance="3015.427007883928" vehicleRefId="null">126690 126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:34:10" end_time="18:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:34:10" trav_time="00:08:28">
                <route type="generic" start_link="408171" end_link="251314" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="shop" link="251314" facility="77137" x="650113.2" y="6860046.8" start_time="18:44:10" end_time="19:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:34:10" trav_time="00:08:28">
                <route type="generic" start_link="251314" end_link="408171" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:44:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="1117138">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >24</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
            <attribute name="carAvailability" class="java.lang.String" >some</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588212</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1136390100003</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >true</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >false</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.4441666666666667" selected="yes">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="06:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="06:58:40" trav_time="00:05:58">
                <route type="links" start_link="408171" end_link="126691" trav_time="00:05:58" distance="3400.8557963805433" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690 126691</route>
            </leg>
            <activity type="work" link="126691" facility="85808" x="647430.5" y="6860756.6" start_time="07:28:40" end_time="15:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="15:58:40" trav_time="00:05:09">
                <route type="links" start_link="126691" end_link="408171" trav_time="00:05:09" distance="2970.5785313919223" vehicleRefId="null">126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:28:40" end_time="18:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:28:40" trav_time="00:07:45">
                <route type="generic" start_link="408171" end_link="405512" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="shop" link="405512" facility="82503" x="649927.3" y="6860261.3" start_time="18:38:40" end_time="19:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:28:40" trav_time="00:07:45">
                <route type="generic" start_link="405512" end_link="408171" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:38:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="1118214">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >40</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588597</attribute>
            <attribute name="employed" class="java.lang.Boolean" >false</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1118620200001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >true</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.9580555555555554" selected="yes">
            <activity type="home" link="244912" facility="home590260" x="653456.081237" y="6863746.566266" end_time="10:48:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car" dep_time="10:48:24" trav_time="00:07:11">
                <route type="links" start_link="244912" end_link="608615" trav_time="00:07:11" distance="3720.6193766258425" vehicleRefId="1118214">244912 560692 606381 606382 606383 125177 125178 265701 665558 665559 666223 649241 293634 293635 161396 424117 659093 157765 157767 157769 157771 157773 6236 6237 6238 666765 194567 194569 194571 194573 194575 194577 194579 194581 194583 527361 125574 125575 125576 538333 608596 498701 608615</route>
            </leg>
            <activity type="outside" link="608615" facility="outside_71" x="655867.0126566273" y="6865733.847801575" end_time="10:54:03" >
            </activity>
            <leg mode="outside" dep_time="10:54:03" trav_time="00:00:00">
                <route type="generic" start_link="608615" end_link="538391" trav_time="00:00:00" distance="53.81682388588168"></route>
            </leg>
            <activity type="outside" link="538391" facility="outside_72" x="655912.9033118412" y="6865761.960046189" end_time="10:54:04" >
            </activity>
            <leg mode="car" dep_time="10:54:04" trav_time="00:00:25">
                <route type="links" start_link="538391" end_link="536876" trav_time="00:00:25" distance="432.14545838613435" vehicleRefId="1118214">538391 538393 538395 538397 536876</route>
            </leg>
            <activity type="outside" link="536876" facility="outside_73" x="655884.8225591567" y="6866141.666875275" end_time="11:18:24" >
            </activity>
            <leg mode="outside" dep_time="11:18:24" trav_time="00:00:00">
                <route type="generic" start_link="536876" end_link="536877" trav_time="00:00:00" distance="0.0"></route>
            </leg>
            <activity type="outside" link="536877" facility="outside_74" x="655884.8225591567" y="6866141.666875275" end_time="11:19:14" >
            </activity>
            <leg mode="car" dep_time="11:19:14" trav_time="00:07:28">
                <route type="links" start_link="536877" end_link="85887" trav_time="00:07:28" distance="7449.705730247254" vehicleRefId="1118214">536877 538398 538396 538394 498692 608598 527359 608597 608614 251798 135070 135071 525671 525641 525626 139149 498299 569781 498300 549975 549961 549962 549120 549116 536055 536043 85853 85854 536072 453490 453473 453412 453457 536073 66252 531990 532001 535555 569763 569764 569744 202426 202425 202424 535572 85887</route>
            </leg>
            <activity type="outside" link="85887" facility="outside_6" x="656620.921626125" y="6859492.595666251" end_time="11:25:48" >
            </activity>
            <leg mode="outside" dep_time="11:25:48" trav_time="00:00:00">
                <route type="generic" start_link="85887" end_link="63872" trav_time="00:00:00" distance="744.9330931635377"></route>
            </leg>
            <activity type="outside" link="63872" facility="outside_7" x="656043.6710628852" y="6859021.737831518" end_time="11:26:23" >
            </activity>
            <leg mode="car" dep_time="11:26:23" trav_time="00:07:49">
                <route type="links" start_link="63872" end_link="296319" trav_time="00:07:49" distance="8034.495548870218" vehicleRefId="1118214">63872 63869 332997 332998 85873 525752 525750 525764 435247 635803 572374 572375 210451 159662 170159 159663 641997 641996 139065 129610 427015 129606 642315 642316 283085 46578 46576 195167 252009 252011 252012 252013 357948 357949 481408 346298 346316 552648 367677 367675 349081 359431 284229 296321 296319</route>
            </leg>
            <activity type="outside" link="296319" facility="outside_262" x="648762.9330018703" y="6858542.741232667" end_time="14:18:24" >
            </activity>
            <leg mode="outside" dep_time="14:18:24" trav_time="00:00:00">
                <route type="generic" start_link="296319" end_link="45314" trav_time="00:00:00" distance="151.9229721657248"></route>
            </leg>
            <activity type="outside" link="45314" facility="outside_285" x="648905.9801573835" y="6858491.573852714" end_time="14:19:27" >
            </activity>
            <leg mode="car" dep_time="14:19:27" trav_time="00:16:28">
                <route type="links" start_link="45314" end_link="551369" trav_time="00:16:28" distance="12346.329980682294" vehicleRefId="1118214">45314 64273 349626 267045 368701 368691 552643 346256 346257 481409 481410 357604 357605 544543 363561 106900 106901 37084 37085 283196 283189 207952 189147 189148 189149 189150 624356 624357 624358 640124 640122 640120 354606 611923 478889 112113 423970 544123 459903 459901 99274 99281 99282 542787 31207 31297 205233 446642 30952 434413 434353 434375 610139 205259 205257 205255 205253 205234 328292 264857 264858 264859 264860 177395 177396 177397 424179 83139 83140 83141 83142 83143 346795 256524 260784 260786 260788 119541 594337 21442 79224 79210 79211 327819 541224 171474 171475 171476 611499 611500 611501 171477 33957 33958 33959 33960 33961 230247 230248 528283 256626 465602 161040 161042 161041 560007 233488 140156 538203 538201 497883 525237 368790 368788 368786 368784 145995 145997 391381 391383 391385 391387 391389 260834 598182 598183 277448 256754 264185 584728 584729 657266 166292 166293 166284 666683 277442 277444 117310 117312 117314 117316 117318 117320 117322 117324 616614 616616 616618 388708 222368 222370 551367 551369</route>
            </leg>
            <activity type="shop" link="551369" facility="48135" x="653826.4" y="6864138.4" start_time="14:48:24" end_time="15:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="15:03:24" trav_time="00:09:44">
                <route type="generic" start_link="551369" end_link="244912" trav_time="00:09:44" distance="700.8792532585932"></route>
            </leg>
            <activity type="home" link="244912" facility="home590260" x="653456.081237" y="6863746.566266" start_time="15:18:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="1118471">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >35</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588682</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1111400100001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >f</attribute>
        </attributes>
        <plan score="-1.2566666666666666" selected="yes">
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" end_time="07:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="07:03:24" trav_time="00:37:42">
                <route type="generic" start_link="93747" end_link="674595" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="work" link="674595" facility="103778" x="650231.7" y="6865783.5" start_time="08:03:24" end_time="17:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="17:03:24" trav_time="00:37:42">
                <route type="generic" start_link="674595" end_link="93747" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" start_time="18:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

</population>

Моя цель - извлечь каждый n-й person. Он не должен быть репрезентативным, то есть его атрибуты не имеют значения. Например, я хочу извлечь каждый 2-й person со всеми их attributes и plans (в моем файле я хотел бы взять, например, каждую 1000-ую). Решение должно выглядеть следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v6.dtd">

<population desc="Switzerland Baseline">

    <attributes>
        <attribute name="coordinateReferenceSystem" class="java.lang.String" >Atlantis</attribute>
    </attributes>

<!-- ====================================================================== -->

    <person id="1117138">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >24</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
            <attribute name="carAvailability" class="java.lang.String" >some</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588212</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1136390100003</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >true</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >false</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.4441666666666667" selected="yes">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="06:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="06:58:40" trav_time="00:05:58">
                <route type="links" start_link="408171" end_link="126691" trav_time="00:05:58" distance="3400.8557963805433" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690 126691</route>
            </leg>
            <activity type="work" link="126691" facility="85808" x="647430.5" y="6860756.6" start_time="07:28:40" end_time="15:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="15:58:40" trav_time="00:05:09">
                <route type="links" start_link="126691" end_link="408171" trav_time="00:05:09" distance="2970.5785313919223" vehicleRefId="null">126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:28:40" end_time="18:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:28:40" trav_time="00:07:45">
                <route type="generic" start_link="408171" end_link="405512" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="shop" link="405512" facility="82503" x="649927.3" y="6860261.3" start_time="18:38:40" end_time="19:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:28:40" trav_time="00:07:45">
                <route type="generic" start_link="405512" end_link="408171" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:38:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>



<!-- ====================================================================== -->

    <person id="1118471">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >35</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588682</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1111400100001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >f</attribute>
        </attributes>
        <plan score="-1.2566666666666666" selected="yes">
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" end_time="07:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="07:03:24" trav_time="00:37:42">
                <route type="generic" start_link="93747" end_link="674595" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="work" link="674595" facility="103778" x="650231.7" y="6865783.5" start_time="08:03:24" end_time="17:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="17:03:24" trav_time="00:37:42">
                <route type="generic" start_link="674595" end_link="93747" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" start_time="18:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>


</population>

Следовательно, на выходе должен снова быть документ XML.

Заранее большое спасибо!

1 Ответ

1 голос
/ 23 апреля 2020

Ниже

import xml.etree.ElementTree as ET
import copy

XML = '''<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v6.dtd">

<population desc="Switzerland Baseline">

    <attributes>
        <attribute name="coordinateReferenceSystem" class="java.lang.String" >Atlantis</attribute>
    </attributes>


<!-- ====================================================================== -->

    <person id="1">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >24</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
            <attribute name="carAvailability" class="java.lang.String" >some</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588212</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1136390100003</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >true</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >false</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.4622222222222222" selected="no">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="07:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="07:04:10" trav_time="00:05:28">
                <route type="links" start_link="408171" end_link="126690" trav_time="00:05:28" distance="3356.007319888538" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690</route>
            </leg>
            <activity type="work" link="126690" facility="86174" x="647377.2" y="6860623.5" start_time="07:34:10" end_time="16:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="16:04:10" trav_time="00:05:20">
                <route type="links" start_link="126690" end_link="408171" trav_time="00:05:20" distance="3015.427007883928" vehicleRefId="null">126690 126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:34:10" end_time="18:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:34:10" trav_time="00:08:28">
                <route type="generic" start_link="408171" end_link="251314" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="shop" link="251314" facility="77137" x="650113.2" y="6860046.8" start_time="18:44:10" end_time="19:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:34:10" trav_time="00:08:28">
                <route type="generic" start_link="251314" end_link="408171" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:44:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

        <plan score="-0.46277777777777773" selected="yes">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="07:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="07:04:10" trav_time="00:05:28">
                <route type="links" start_link="408171" end_link="126690" trav_time="00:05:28" distance="3356.007319888538" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690</route>
            </leg>
            <activity type="work" link="126690" facility="86174" x="647377.2" y="6860623.5" start_time="07:34:10" end_time="16:04:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="16:04:10" trav_time="00:05:20">
                <route type="links" start_link="126690" end_link="408171" trav_time="00:05:20" distance="3015.427007883928" vehicleRefId="null">126690 126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:34:10" end_time="18:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:34:10" trav_time="00:08:28">
                <route type="generic" start_link="408171" end_link="251314" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="shop" link="251314" facility="77137" x="650113.2" y="6860046.8" start_time="18:44:10" end_time="19:34:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:34:10" trav_time="00:08:28">
                <route type="generic" start_link="251314" end_link="408171" trav_time="00:08:28" distance="609.6278175068891"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:44:10" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="2">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >24</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >some</attribute>
            <attribute name="carAvailability" class="java.lang.String" >some</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588212</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1136390100003</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >true</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >false</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.4441666666666667" selected="yes">
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" end_time="06:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="06:58:40" trav_time="00:05:58">
                <route type="links" start_link="408171" end_link="126691" trav_time="00:05:58" distance="3400.8557963805433" vehicleRefId="null">408171 592317 186333 186331 572274 572275 572270 494663 64268 64269 64270 64271 588731 115160 243298 413672 461772 189464 521042 521043 521044 521045 521046 521047 521048 521049 137033 137034 137035 548186 154670 154671 154672 154673 154674 108981 248857 248859 248861 248863 297334 297336 297338 297340 126690 126691</route>
            </leg>
            <activity type="work" link="126691" facility="85808" x="647430.5" y="6860756.6" start_time="07:28:40" end_time="15:58:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car_passenger" dep_time="15:58:40" trav_time="00:05:09">
                <route type="links" start_link="126691" end_link="408171" trav_time="00:05:09" distance="2970.5785313919223" vehicleRefId="null">126691 109030 109031 109032 109034 109035 159370 159371 159372 108978 108979 109001 180079 232882 232880 232878 232876 232874 232872 232870 232868 232866 461761 461759 461757 251592 251590 251588 251586 251584 440591 64245 537810 289569 289542 408171</route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="16:28:40" end_time="18:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="18:28:40" trav_time="00:07:45">
                <route type="generic" start_link="408171" end_link="405512" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="shop" link="405512" facility="82503" x="649927.3" y="6860261.3" start_time="18:38:40" end_time="19:28:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="19:28:40" trav_time="00:07:45">
                <route type="generic" start_link="405512" end_link="408171" trav_time="00:07:45" distance="558.442415424602"></route>
            </leg>
            <activity type="home" link="408171" facility="home589659" x="649660.436426" y="6859924.676838" start_time="19:38:40" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="3">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >40</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588597</attribute>
            <attribute name="employed" class="java.lang.Boolean" >false</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1118620200001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >true</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >m</attribute>
        </attributes>
        <plan score="-0.9580555555555554" selected="yes">
            <activity type="home" link="244912" facility="home590260" x="653456.081237" y="6863746.566266" end_time="10:48:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="car" dep_time="10:48:24" trav_time="00:07:11">
                <route type="links" start_link="244912" end_link="608615" trav_time="00:07:11" distance="3720.6193766258425" vehicleRefId="1118214">244912 560692 606381 606382 606383 125177 125178 265701 665558 665559 666223 649241 293634 293635 161396 424117 659093 157765 157767 157769 157771 157773 6236 6237 6238 666765 194567 194569 194571 194573 194575 194577 194579 194581 194583 527361 125574 125575 125576 538333 608596 498701 608615</route>
            </leg>
            <activity type="outside" link="608615" facility="outside_71" x="655867.0126566273" y="6865733.847801575" end_time="10:54:03" >
            </activity>
            <leg mode="outside" dep_time="10:54:03" trav_time="00:00:00">
                <route type="generic" start_link="608615" end_link="538391" trav_time="00:00:00" distance="53.81682388588168"></route>
            </leg>
            <activity type="outside" link="538391" facility="outside_72" x="655912.9033118412" y="6865761.960046189" end_time="10:54:04" >
            </activity>
            <leg mode="car" dep_time="10:54:04" trav_time="00:00:25">
                <route type="links" start_link="538391" end_link="536876" trav_time="00:00:25" distance="432.14545838613435" vehicleRefId="1118214">538391 538393 538395 538397 536876</route>
            </leg>
            <activity type="outside" link="536876" facility="outside_73" x="655884.8225591567" y="6866141.666875275" end_time="11:18:24" >
            </activity>
            <leg mode="outside" dep_time="11:18:24" trav_time="00:00:00">
                <route type="generic" start_link="536876" end_link="536877" trav_time="00:00:00" distance="0.0"></route>
            </leg>
            <activity type="outside" link="536877" facility="outside_74" x="655884.8225591567" y="6866141.666875275" end_time="11:19:14" >
            </activity>
            <leg mode="car" dep_time="11:19:14" trav_time="00:07:28">
                <route type="links" start_link="536877" end_link="85887" trav_time="00:07:28" distance="7449.705730247254" vehicleRefId="1118214">536877 538398 538396 538394 498692 608598 527359 608597 608614 251798 135070 135071 525671 525641 525626 139149 498299 569781 498300 549975 549961 549962 549120 549116 536055 536043 85853 85854 536072 453490 453473 453412 453457 536073 66252 531990 532001 535555 569763 569764 569744 202426 202425 202424 535572 85887</route>
            </leg>
            <activity type="outside" link="85887" facility="outside_6" x="656620.921626125" y="6859492.595666251" end_time="11:25:48" >
            </activity>
            <leg mode="outside" dep_time="11:25:48" trav_time="00:00:00">
                <route type="generic" start_link="85887" end_link="63872" trav_time="00:00:00" distance="744.9330931635377"></route>
            </leg>
            <activity type="outside" link="63872" facility="outside_7" x="656043.6710628852" y="6859021.737831518" end_time="11:26:23" >
            </activity>
            <leg mode="car" dep_time="11:26:23" trav_time="00:07:49">
                <route type="links" start_link="63872" end_link="296319" trav_time="00:07:49" distance="8034.495548870218" vehicleRefId="1118214">63872 63869 332997 332998 85873 525752 525750 525764 435247 635803 572374 572375 210451 159662 170159 159663 641997 641996 139065 129610 427015 129606 642315 642316 283085 46578 46576 195167 252009 252011 252012 252013 357948 357949 481408 346298 346316 552648 367677 367675 349081 359431 284229 296321 296319</route>
            </leg>
            <activity type="outside" link="296319" facility="outside_262" x="648762.9330018703" y="6858542.741232667" end_time="14:18:24" >
            </activity>
            <leg mode="outside" dep_time="14:18:24" trav_time="00:00:00">
                <route type="generic" start_link="296319" end_link="45314" trav_time="00:00:00" distance="151.9229721657248"></route>
            </leg>
            <activity type="outside" link="45314" facility="outside_285" x="648905.9801573835" y="6858491.573852714" end_time="14:19:27" >
            </activity>
            <leg mode="car" dep_time="14:19:27" trav_time="00:16:28">
                <route type="links" start_link="45314" end_link="551369" trav_time="00:16:28" distance="12346.329980682294" vehicleRefId="1118214">45314 64273 349626 267045 368701 368691 552643 346256 346257 481409 481410 357604 357605 544543 363561 106900 106901 37084 37085 283196 283189 207952 189147 189148 189149 189150 624356 624357 624358 640124 640122 640120 354606 611923 478889 112113 423970 544123 459903 459901 99274 99281 99282 542787 31207 31297 205233 446642 30952 434413 434353 434375 610139 205259 205257 205255 205253 205234 328292 264857 264858 264859 264860 177395 177396 177397 424179 83139 83140 83141 83142 83143 346795 256524 260784 260786 260788 119541 594337 21442 79224 79210 79211 327819 541224 171474 171475 171476 611499 611500 611501 171477 33957 33958 33959 33960 33961 230247 230248 528283 256626 465602 161040 161042 161041 560007 233488 140156 538203 538201 497883 525237 368790 368788 368786 368784 145995 145997 391381 391383 391385 391387 391389 260834 598182 598183 277448 256754 264185 584728 584729 657266 166292 166293 166284 666683 277442 277444 117310 117312 117314 117316 117318 117320 117322 117324 616614 616616 616618 388708 222368 222370 551367 551369</route>
            </leg>
            <activity type="shop" link="551369" facility="48135" x="653826.4" y="6864138.4" start_time="14:48:24" end_time="15:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="15:03:24" trav_time="00:09:44">
                <route type="generic" start_link="551369" end_link="244912" trav_time="00:09:44" distance="700.8792532585932"></route>
            </leg>
            <activity type="home" link="244912" facility="home590260" x="653456.081237" y="6863746.566266" start_time="15:18:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

<!-- ====================================================================== -->

    <person id="4">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >35</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588682</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1111400100001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >f</attribute>
        </attributes>
        <plan score="-1.2566666666666666" selected="yes">
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" end_time="07:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="07:03:24" trav_time="00:37:42">
                <route type="generic" start_link="93747" end_link="674595" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="work" link="674595" facility="103778" x="650231.7" y="6865783.5" start_time="08:03:24" end_time="17:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="17:03:24" trav_time="00:37:42">
                <route type="generic" start_link="674595" end_link="93747" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" start_time="18:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>

        <person id="5">
        <attributes>
            <attribute name="age" class="java.lang.Integer" >35</attribute>
            <attribute name="bikeAvailability" class="java.lang.String" >none</attribute>
            <attribute name="carAvailability" class="java.lang.String" >none</attribute>
            <attribute name="censusId" class="java.lang.Integer" >588682</attribute>
            <attribute name="employed" class="java.lang.Boolean" >true</attribute>
            <attribute name="hasLicense" class="java.lang.String" >yes</attribute>
            <attribute name="htsId" class="java.lang.Long" >1111400100001</attribute>
            <attribute name="isOutside" class="java.lang.Boolean" >false</attribute>
            <attribute name="isPassenger" class="java.lang.Boolean" >false</attribute>
            <attribute name="ptSubscription" class="java.lang.Boolean" >true</attribute>
            <attribute name="sex" class="java.lang.String" >f</attribute>
        </attributes>
        <plan score="-1.2566666666666666" selected="yes">
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" end_time="07:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="07:03:24" trav_time="00:37:42">
                <route type="generic" start_link="93747" end_link="674595" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="work" link="674595" facility="103778" x="650231.7" y="6865783.5" start_time="08:03:24" end_time="17:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
            <leg mode="walk" dep_time="17:03:24" trav_time="00:37:42">
                <route type="generic" start_link="674595" end_link="93747" trav_time="00:37:42" distance="2715.3893856790746"></route>
            </leg>
            <activity type="home" link="93747" facility="home590412" x="648223.682345" y="6865208.358376" start_time="18:03:24" >
                <attributes>
                    <attribute name="innerParis" class="java.lang.Boolean" >true</attribute>
                </attributes>
            </activity>
        </plan>

    </person>


</population>'''

N = 2
root = ET.fromstring(XML)
new_root = copy.deepcopy(root)
new_root.clear()
new_root.append(root.find('.//attributes'))
persons = root.findall('.//person')
for idx,person in enumerate(persons):
    if idx % N == 0:
        new_root.append(person)
ET.dump(new_root)
...