pythonxml解析需要纠正while循环

2024-04-28 08:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

对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美元。此XML块显示,每12个月(重复间隔),租金将为6.80美元/平方英尺(3.00美元基础+3.80美元升级)。接下来的12个月将是10.60美元(6.80美元+3.80美元)。每年,每平方英尺的金额将增加3.80美元,直到任期的第61个月。届时,剩余期限的租金将增加1.00美元/平方英尺。整个租期为120个月。你知道吗

我的结果包括基于第一次升级(3.80/平方英尺)的114个结果,然后是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)

Tags: textidiffindintdtsescescalation