Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 使用lxml向xml文件添加新值_Python 2.7_Lxml - Fatal编程技术网

Python 2.7 使用lxml向xml文件添加新值

Python 2.7 使用lxml向xml文件添加新值,python-2.7,lxml,Python 2.7,Lxml,我有以下xml文件: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <smrtpipeSettings> <protocol version="2.2.0" id="RS_ReadsOfInsert.1" editable="false"> <param name="name" label="Protocol Name"> <va

我有以下xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<smrtpipeSettings>
    <protocol version="2.2.0" id="RS_ReadsOfInsert.1" editable="false">
        <param name="name" label="Protocol Name">
            <value>RS_ReadsOfInsert</value>
            <input type="text"/>
            <rule required="true"/>
        </param>
        <param name="description">
            <value>Generates single-molecule consensus reads from the insert template, filters output by quality and length, and optionally splits by barcode. Output in FASTA and cmp.h5 format.
      </value>
            <textarea></textarea>
        </param>
        <param name="version" hidden="true">
            <value>1</value>
            <input type="text"/>
            <rule type="digits" required="true" min="1.0"/>
        </param>
        <param name="state" hidden="true">
            <value>active</value>
            <input value="active" type="radio"/>
            <input value="inactive" type="radio"/>
        </param>
        <param name="control" hidden="true">
            <value></value>
        </param>
        <param name="fetch" hidden="true">
            <value>common/protocols/preprocessing/Fetch.1.xml</value>
        </param>
        <param name="Filtering">
            <value>common/protocols/consensus/ReadsOfInsert.1.xml</value>
            <select multiple="true">
                <import extension="xml" contentType="text/directory">common/protocols/consensus</import>
            </select>
        </param>
        <param name="barcode" editableInJob="true">
            <value>common/protocols/barcode/NoBarcode.1.xml</value>
            <select multiple="false">
                <import extension="xml" contentType="text/directory">common/protocols/barcode</import>
            </select>
        </param>
    </protocol>
    <moduleStage name="fetch" editable="true">
        <module label="Fetch v1" id="P_Fetch" editableInJob="true">
            <description>Sets up inputs</description>
        </module>
    </moduleStage>
    <moduleStage name="Filtering" editable="true">
        <module label="Reads Of Insert" id="P_CCS" editableInJob="true">
            <description>Generates consensus sequences from single molecules.</description>
            <param name="minFullPasses" label="Minimum Full Passes">
                <value>4</value>
                <title>The minimum number of full-length passes over the insert DNA for the read to be included.</title>
                <input type="text"/>
                <rule type="digits" min="0.0" message="Value must be an integer between 0 and 10" max="10.0"/>
            </param>
            <param name="minPredictedAccuracy" label="Minimum Predicted Accuracy">
                <value>90</value>
                <title>Minimum Read Quality of Insert</title>
                <input type="text"/>
                <rule type="digits" min="70.0" message="Value must be between 70 and 100" max="100.0"/>
            </param>
            <param name="minLength" label="Minimum Read Length of Insert (in bases)">
                <value>400</value>
                <title>The Minimum Read Length of Insert (in bases). Default: No Minimum Length
      </title>
                <input type="text"/>
                <rule type="digits" min="1.0" message="Value must be greater than 0"/>
            </param>
            <param name="maxLength" label="Maximum Read Length of Insert (in bases)">
                <title>The Maximum Read Length of Insert (in bases). Default: No Limit</title>
                <input type="text"/>
                <rule type="digits" min="1.0" message="Value must be greater than 0"/>
            </param>
        </module>
    </moduleStage>
    <moduleStage name="barcode" editable="true"/>
    <fileName>RS_ReadsOfInsert.1.xml</fileName>
</smrtpipeSettings>
现在我想把这个值改成其他几个值,比如:3,12,20和25。如何将这些值复制回xml文件

我试过这个:

for nrpass in doc.xpath("//param[@name='minFullPasses']/value/text()"):
nrpass.attrib['value'] = 8

但这只会让我犯错误。任何提示都将不胜感激

需要更改的是父节点的文本:

for nrpass in doc.xpath("//param[@name='minFullPasses']/value/text()"):
    parent = nrpass.getparent()
    parent.text = '8'
或者更简单:

for node in doc.xpath("//param[@name='minFullPasses']/value"):
    node.text = '8'

谢谢,这很有效!你能告诉我如何写回xml吗?因为这也不适用于me@SanderVanderZeeuw:您可以使用以下命令获取xml文档的最终状态:
print etree.tostring(doc)
for nrpass in doc.xpath("//param[@name='minFullPasses']/value/text()"):
    parent = nrpass.getparent()
    parent.text = '8'
for node in doc.xpath("//param[@name='minFullPasses']/value"):
    node.text = '8'