【问题标题】:Python ElementTree how do send the value of a variable to xml outputPython ElementTree如何将变量的值发送到xml输出
【发布时间】:2015-04-16 23:31:20
【问题描述】:

我想用lastrun 日期属性中的当前日期更新xml 文件。 下面的代码导致+ str(mprocessdate) +,我希望它说2015-04-16

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

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')

【问题讨论】:

  • 你试过运行你的代码吗?当你这样做时会发生什么?

标签: python xml elementtree


【解决方案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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2012-10-01
    • 2021-02-21
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多