2015-04-16 37 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')