如何从其构造函数设置 ElementTree Element 的文本字段?或者,在下面的代码中,为什么 root.text 的第二个打印是 None?
import xml.etree.ElementTree as ET
root = ET.fromstring("<period units='months'>6</period>")
ET.dump(root)
print root.text
root=ET.Element('period', {'units': 'months'}, text='6')
ET.dump(root)
print root.text
root=ET.Element('period', {'units': 'months'})
root.text = '6'
ET.dump(root)
print root.text
这里的输出:
<period units="months">6</period>
6
<period text="6" units="months" />
None
<period units="months">6</period>
6