我被困在一段Python代码上。
从XML文件中,我们正在以下代码中成功解析数据,但while循环和关联变量除外。我们需要在整个租用期内按月将整个租期表加载到SQL中。租金总是在每月的第一天计费,但金额会在不同的时间根据租约逐步升级。目的是在每个计费月份返回一行,并记录每个月的租金日期(YYYY-MM-DD)。如果租期为60个月,并且在第25个月出现租金上涨,则我们需要显示60行,其中前两年重复24次,其余重复36次。该方案需要灵活以适应某些和其他一些可变条件的年度增长。
有人可以指出我在While循环中出错的地方,以获得所需的结果吗?
import xml.etree.ElementTree as ET
import pyodbc
import dateutil.relativedelta as rd
import dateutil.parser as pr
tree = ET.parse('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")
dstart = pr.parse(dts.find("CommencementDate").text)
dterm = dts.find("LeaseTerm").text
darea = dts.find("RentableArea").text
for brrent in dts.findall("BaseRents/BaseRent"):
brid = brrent.get("ID")
begmo = int(brrent.find("BeginIn").text)
if brrent.find("Duration").text is not None:
duration = int(brrent.find("Duration").text)
else:
duration = 0
brentamt = brrent.find("Rent").text
brper = brrent.find("Period").text
perst = dstart + rd.relativedelta(months=begmo-1)
perend = perst + rd.relativedelta(months=duration-1)
billmocount = begmo
while billmocount < duration:
monthnum = billmocount
billmocount += 1
billmo = perst
while billmo < perend:
billper = billmo
billmo += rd.relativedelta(months=1)
if dealid == "706880":
print(dealid, dtid, brid, begmo, dstart, dterm, darea, brentamt, brper, duration, perst, perend, \
monthnum, billper)
我得到的结果如下:
706880 4278580 45937180 1 2018-01-01 00:00:00 60 6200 15.0 rsf/year 36 2018-01-01 00:00:00 2020-12-01 00:00:00 35 2020-11-01 00:00:00
706880 4278580 45937181 37 2018-01-01 00:00:00 60 6200 18.0 rsf/year 24 2021-01-01 00:00:00 2022-12-01 00:00:00 35 2022-11-01 00:00:00
答案 0 :(得分:0)
我遇到的问题仅仅是print语句的缩进。通过缩进以下文本,我能够获得预期的结果:
如果Dealid ==“ 706880”: 打印(dealid,dtid,brid,begmo,dstart,dterm,darea,brentamt,brper,duration,perst,perend,\ monthnum,billper)