Skip to content Skip to sidebar Skip to footer

Replace Multiple Texts With Their Corresponding Texts In Xml Using Python - Part 2 -

I would like to replace a text of aliasname element with a text from name if there is no corresponding key in the dictionary. here is the xml that I am working on now. -

Solution 1:

I guess (seeing that the current behaviour is not what you want) you'd like to replace a text of <AliasName> element with a text from <Name> if there is no corresponding key in the dictionary. Change this line (which doesn't change <AliasName> if there is no key):

alias.text = jp2en.get(name.text.strip(), alias.text)

To:

alias.text = jp2en.get(name.text.strip(), name.text)

If key values should come from <AliasName> instead of <Name> then replace the above line by:

ifalias.textisnot None:
    alias.text = jp2en.get(alias.text.strip(), name.text)
 else:
    alias.text = name.text

Post a Comment for "Replace Multiple Texts With Their Corresponding Texts In Xml Using Python - Part 2 -"