I have an xml file that contains this:
<supported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
<lang><![CDATA[de_DE]]></lang>
</supported-languages>
<2ndsupported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
<lang><![CDATA[de_DE]]></lang>
</2ndsupported-languages>
I only want to delete any line that contains de_DE
, and save the file.
So far I have this:
import fileinput
import sys
file = "C:\\Users\Desktop\file.xml"
searchExp = "de_DE"
replaceExp = ""
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
replaceAll(file,searchExp,replaceExp)
Close, but not really. It will will search for "de_DE", but will only replace that with <null>
. This is the result:
<supported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
<lang><![CDATA[]]></lang>
</supported-languages>
<2ndsupported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
<lang><![CDATA[]]></lang>
</2ndsupported-languages>
I want my results to look like this
<supported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
</supported-languages>
<2ndsupported-languages>
<lang><![CDATA[en_US]]></lang>
<lang><![CDATA[es_ES]]></lang>
</2ndsupported-languages>
How do I do this?
I tried to import re
and then replace pattern
with
pattern = "^.*de_DE.*$"
but that did not work.