Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lxml通过id标记查找元素_Python_Xml_Xml Parsing_Lxml - Fatal编程技术网

Python lxml通过id标记查找元素

Python lxml通过id标记查找元素,python,xml,xml-parsing,lxml,Python,Xml,Xml Parsing,Lxml,我正在开发一个python程序,用于保存储藏室的库存。在XML文档中,将保留墨粉量,我希望我的python程序能够添加、删除和显示不同打印机和不同颜色的墨粉量 我的XML如下所示: <?xml version="1.0"?> <printer> <t id="095205615111"> <!-- 7545 Magenta --> <toner>7545 Magenta Toner</toner>

我正在开发一个python程序,用于保存储藏室的库存。在XML文档中,将保留墨粉量,我希望我的python程序能够添加、删除和显示不同打印机和不同颜色的墨粉量

我的XML如下所示:

<?xml version="1.0"?>
<printer>
    <t id="095205615111"> <!-- 7545 Magenta -->
        <toner>7545 Magenta Toner</toner>
        <amount>3</amount>
    </t>
    <t id="095205615104"> <!-- 7545 Yellow -->
        <toner>7545 Yellow Toner</toner>
        <amount>7</amount>
    </t>
</printer>

7545洋红色碳粉
3.
7545黄色碳粉
7.
id是我们用于库存的条形码中的编号

到目前为止,我希望我的程序使用以下步骤:

  • 检查id是否存在(id值是python程序中的一个变量,从txt文件中的内容导出)

  • 将xml文档中amount的值更改为+1或-1

  • 无论我尝试什么,它都不会完全起作用。你对我能用什么有什么建议吗

    检查id是否存在

    您可以通过构造一个XPath表达式来检查
    @id
    属性值来解决这个问题

    将xml文档中amount的值更改为+1或-1

    通过特定的
    id
    定位
    t
    节点后,可以使用
    find()
    定位内部
    amount
    节点。然后,您可以获取
    .text
    ,将其转换为整数,对其进行更改,再转换回字符串并设置
    .text
    属性

    工作示例:

    from lxml import etree
    
    data = """<?xml version="1.0"?>
    <printer>
        <t id="095205615111"> <!-- 7545 Magenta -->
            <toner>7545 Magenta Toner</toner>
            <amount>3</amount>
        </t>
        <t id="095205615104"> <!-- 7545 Yellow -->
            <toner>7545 Yellow Toner</toner>
            <amount>7</amount>
        </t>
    </printer>"""
    
    
    root = etree.fromstring(data)
    
    toner_id = "095205615111"
    
    # find a toner
    results = root.xpath("//t[@id = '%s']" % toner_id)
    if not results:
        raise Exception("Toner does not exist")
    
    toner = results[0]
    
    # change the amount
    amount = toner.find("amount")
    amount.text = str(int(amount.text) + 1)
    
    print(etree.tostring(root))
    
    从lxml导入etree
    data=”“”
    7545洋红色碳粉
    3.
    7545黄色碳粉
    7.
    """
    root=etree.fromstring(数据)
    碳粉_id=“095205615111”
    #找到墨粉
    results=root.xpath(//t[@id='%s']%s\u id)
    如果没有结果:
    升起异常(“碳粉不存在”)
    墨粉=结果[0]
    #更改金额
    数量=碳粉。查找(“数量”)
    amount.text=str(int(amount.text)+1)
    打印(etree.tostring(根))
    
    您还可以使用它来简化数据类型的处理:

    from lxml import objectify, etree
    
    data = """<?xml version="1.0"?>
    <printer>
        <t id="095205615111"> <!-- 7545 Magenta -->
            <toner>7545 Magenta Toner</toner>
            <amount>3</amount>
        </t>
        <t id="095205615104"> <!-- 7545 Yellow -->
            <toner>7545 Yellow Toner</toner>
            <amount>7</amount>
        </t>
    </printer>"""
    
    
    root = objectify.fromstring(data)
    
    toner_id = "095205615111"
    
    # find a toner
    results = root.xpath("//t[@id = '%s']" % toner_id)
    if not results:
        raise Exception("Toner does not exist")
    
    toner = results[0]
    
    # change the amount
    toner.amount += 1
    
    # dump the tree object back to XML string
    objectify.deannotate(root)
    etree.cleanup_namespaces(root)
    print(etree.tostring(root))
    

    发布你的代码,不管你想什么tried@Diego没问题。试着把你下次试过的东西贴出来——这会引起更多的注意,帮助你更好地理解你遇到的问题。此外,考虑接受一个答案来解决这个话题。谢谢
    toner.amount += 1