Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

I have XML file I want change its element value through python. change its value if "No" changed to "NA", and other than "Yes"

Data.xml

<?xml version='1.0' encoding='UTF-8'?>
<inno:dom xmlns:inno="http://innodatalabs.com/innodom">
    <inno:meta>
        <inno:datapoint idref="text-61">Yes</inno:datapoint>
        <inno:datapoint idref="text-64">No</inno:datapoint>
        <inno:datapoint idref="text-67">Personal Information</inno:datapoint>
        <inno:datapoint idref="text-68">Non-Personal Information</inno:datapoint>
    </inno:meta>
</inno:dom>

Output should like Data.xml

<?xml version='1.0' encoding='UTF-8'?>
<inno:dom xmlns:inno="http://innodatalabs.com/innodom">
    <inno:meta>
        <inno:datapoint idref="text-61">Yes</inno:datapoint>
        <inno:datapoint idref="text-64">NA</inno:datapoint>
        <inno:datapoint idref="text-67">Yes</inno:datapoint>
        <inno:datapoint idref="text-68">Yes</inno:datapoint>
    </inno:meta>
</inno:dom>

*if the value is "No" change to "NA" if the value is other than "Yes" or "No" change to "Yes" *

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use regex for quick results:

import re

x = """
<?xml version='1.0' encoding='UTF-8'?>
<inno:dom xmlns:inno="http://innodatalabs.com/innodom">
    <inno:meta>
        <inno:datapoint idref="text-61">Yes</inno:datapoint>
        <inno:datapoint idref="text-64">No</inno:datapoint>
        <inno:datapoint idref="text-67">Personal Information</inno:datapoint>
        <inno:datapoint idref="text-68">Non-Personal Information</inno:datapoint>
    </inno:meta>
</inno:dom>
"""
result = re.sub(r">([^<
]+)</", lambda x: ">NA</" if x.group() == ">No</" else ">Yes</", x)
print(result)

But as @mzjn pointed out, in general it's not a great idea to change XML files using regex.

Please see ElementTree and how to modify XML files.

Example:

import xml.etree.ElementTree as ET

if __name__ == "__main__":
    namespace_uri = "http://innodatalabs.com/innodom"
    ET.register_namespace('inno', namespace_uri)
    tree = ET.parse('test.xml')
    root = tree.getroot()
    for child in root.iter("{{{0}}}datapoint".format(namespace_uri)):
        text = child.text
        if text == "No":
            child.text = "NA"
        else:
            child.text = "Yes"
    tree.write("output.xml")

where test.xml is your example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...