2015-04-16 36 views
-1

我想用lastrun date属性中的当前日期更新xml文件。 以下代码是+ str(mprocessdate) +,我想说的是2015-04-16Python ElementTree如何将变量的值发送到xml输出

我的代码有什么问题?为什么我会得到那个字符串而不是实际的日期?

company1.xml

<corp> 
<lastrun date="20150123" /> 
<company id="18888802223"> 
    <name>South Plantation</name> 
    <P_DNIS>99603</P_DNIS> 
    <Tracking_Phone>+18888802223</Tracking_Phone> 
    <Account>South Plantation</Account> 
    <AppendValue> Coupon</AppendValue> 
    <InsertCoupon>Y</InsertCoupon> 
</company> 
</corp> 

脚本

import datetime 
from xml.etree import ElementTree as ET 

mprocessdate = datetime.date.today() 
print (mprocessdate) 
tree = ET.parse("company1.xml") 
mlastrun = tree.find('lastrun') 
mlastrun.set('date', '+ str(mprocessdate) + ') 
tree.write('company.xml') 
+0

您是否尝试过运行您的代码?当你做什么时会发生什么? – Vorticity

回答

1

离开关+,只是把变量名。

import datetime 
from xml.etree import ElementTree as ET 

mprocessdate = datetime.date.today() 
print (mprocessdate) 

tree = ET.parse("company.xml") 

mlastrun = tree.find('lastrun') 

mlastrun.set('date', str(mprocessdate)) 

tree.write('company.xml')