Python XML解析-需要更正while循环

时间:2018-07-15 20:28:58

标签: python xml python-3.x while-loop

Python的新手。我正在解析一个XML文件,以下代码返回了不良结果。我能理解为什么我要得到结果-XML在这笔交易中有两次升级,而且每套都得到了结果。我需要更新代码的帮助,以便仅返回XML中每次升级的月租金:

<RentEscalations>
    <RentEscalation ID="354781">
        <BeginIn>7</BeginIn>
        <Escalation>3.8</Escalation>
        <RecurrenceInterval>12</RecurrenceInterval>
        <EscalationType>bump</EscalationType>
    </RentEscalation>
    <RentEscalation ID="354782">
        <BeginIn>61</BeginIn>
        <Escalation>1.0</Escalation>
        <RecurrenceInterval>12</RecurrenceInterval>
        <EscalationType>bump</EscalationType>
    </RentEscalation>
</RentEscalations>

前6个月的租金为$ 3.00 /平方英尺。该XML块显示,每12个月(RecurrenceInterval),租金将为$ 6.80 /平方呎($ 3.00基础+ $ 3.80递增)。接下来的十二个月将为$ 10.60($ 6.80 + 3.80)。到本期的第61个月,每年每平方英尺的金额将增加$ 3.80。届时,剩余期间的租金将增加$ 1.00 /平方呎。整个租期为120个月。

我的结果包括基于第一个升级的114个结果(3.80 /平方呎),其后是114行,显示租金开始时为$ 3.00 /平方呎,每年递增$ 1.00 /平方呎。

感谢您的帮助!

import xml.etree.ElementTree as ET
import pyodbc
import dateutil.relativedelta as rd
import datetime as dt

tree = ET.parse('C:\\FileLocation\\DealData.xml')
root = tree.getroot()

for deal in root.findall("Deals"):
    for dl in deal.findall("Deal"):
        dealid = dl.get("DealID")
        for dts in dl.findall("DealTerms/DealTerm"):
            dtid = dts.get("ID")
            darea = float(dts.find("RentableArea").text)
            dterm = int(dts.find("LeaseTerm").text)
        for brrent in dts.findall("BaseRents/BaseRent"):
            brid = brrent.get("ID")
            rent = float(brrent.find("Rent").text)
            darea = float(dts.find("RentableArea").text)
            per = brrent.find("Period").text
            dtstart = dts.find("CommencementDate").text
            startyr = int(dtstart[0:4])
            startmo = int(dtstart[5:7])
            startday = int(dtstart[8:])
            start = dt.date(startyr, startmo, startday)
            end = start + rd.relativedelta(months=dterm)
            if brrent.find("Duration").text is None:
                duration = 0
            else:
                duration = int(brrent.find("Duration").text)
            termbal = dterm - duration
        for resc in dts.findall("RentEscalations/RentEscalation"):
            rescid = resc.get("ID")
            esctype = resc.find("EscalationType").text
            begmo = int(resc.find("BeginIn").text)
            esc = float(resc.find("Escalation").text)
            intrvl = int(resc.find("RecurrenceInterval").text)
            if intrvl != 0:
                pers = termbal / intrvl
            else:
                pers = 0
            escst = start + rd.relativedelta(months=begmo - 1)

            i = 0
            x = begmo
            newrate = rent
            while i < termbal:
                billdt = escst + rd.relativedelta(months=i)
                if per == "rsf/year":
                    monthlyamt = (newrate + esc) * darea / 12.0
                if per == "month":
                    monthlyamt = newrate + esc
                if per == "year":
                    monthlyamt = (newrate + esc) / 12.0
                if per == "rsf/month":
                    monthlyamt = (newrate + esc) * darea
                try:
                    if i % intrvl == 0:
                        level = x + 1
                        newrent = monthlyamt
                        x += 1
                        newrate += esc
                    else:
                        level = x
                except ZeroDivisionError:
                    break
                i += 1

                if dealid == "1254278":
                    print(dealid, dtid, rescid, dterm, darea, escst, rent, intrvl, esctype, termbal, \
                    monthlyamt, billdt, pers, level, newrate, newrent)

0 个答案:

没有答案