我使用lxml基于某些数据构建XML。
root = etree.Element('sentences')
for item in aspectSentenceList:
print item
sentenceTag = etree.SubElement(root, "sentence", id=item[0])
textTag = etree.SubElement(sentenceTag, "text", text=item[1])
aspectTermsTag = etree.SubElement(sentenceTag, "aspectTerms")
for asp in item[2]:
aspectTermTag = etree.SubElement(aspectTermsTag, "aspectTerm", term = asp[0], frm = asp[1], to = asp[2])
捕获量在最后一行。有三个属性,术语,从和到。问题是,python不允许我使用"来自"除常规导入之外的任何任务的关键字。虽然我已经使用 frm 代替来自进行了临时解决,但稍后会替换所有此类字符串。但是,如果没有无信任的黑客,我怎么能这样做呢?
答案 0 :(得分:1)
哦在发布问题后的五分钟内得到了它。文档提到属性是python词典。我更新了最后一行: -
aspectTermTag = etree.SubElement(aspectTermsTag, "aspectTerm", {"term":asp[0], "from": asp[1], "to":asp[2]})