我想将十几个属性分配给一个 lxml 节点。lambda 用于简化输入。注释赋值操作没问题,但使用 lambda sa 会抛出异常。有没有人知道在这种情况下可以使用 lambda 吗?提前感谢!
sa = set_attr = lambda n, v: eval("etp.xpath(u'/Recipe_Definition/%s')[0].text = u'%s'" % (n, v), {u'etp':etp})
#etp.xpath(u'/Recipe_Definition/Author')[0].text = rc.author
#etp.xpath(u'/Recipe_Definition/Comment')[0].text = rc.comment
sa(u'Author', rc.author)
sa(u'Comment', rc.comment)
例外情况是:
sa = set_attr = lambda n, v: eval("etp.xpath(u'/Recipe_Definition/%s')[0].text = u'%s'" % (n, v), {u'etp':etp})
File "<string>", line 1
etp.xpath(u'/Recipe_Definition/Author')[0].text = u'admin'