Skip to content
Advertisement

How to replace an XML node via python

I am new in python and I have a – maybe – stupid problem with XML files (yep, I’ve tried to google the solution but without results).

I have to write a program which will replace/switch two things, so first of all, here is the XML data, it looks like this:

JavaScript

How in python i could change some_name to be in data=''? So it should be like this:

JavaScript

If it’s a stupid question, sorry about that, but I truly googled it and I cannot find a solution.

UPDATE: Here is the few lines of python code that I wrote:

JavaScript

And here is true XML data:

JavaScript

There are many of the signs. Just <data>...</data> name should be in name="..."

Alright, so here are the content of files. First of all I am generating CSV file:

JavaScript

Then, I want to change it to be a XML:

JavaScript

Finally, I want to have parsed XML, so as I wrote above, I have trying to use this kind of script:

JavaScript

So my intention is here – i think :) -:

JavaScript

name="" should be the same as <Name> HERE </Name>

Advertisement

Answer

Below:

JavaScript

output

JavaScript

The other way (set the value of the name attribute with the text of data)

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement