I have hit a large brick wall attempting to solve my current problem. I have 100’s of xml files and need to modify them based on a list of values. The values in the list should be removed from the xml’s. I can’t simply delete the values as it causes the xml’s to be unparsable. So I have to replace the line with a differing field.
I have tried data.replace to replace the string but I have to put a variable in the ID field and VALUE filed , so far it doesn’t take it.
var1 = 9.ZZXX
var2 = ZZXX
with open(i_path0, 'r') as f:
data = f.read()
data = f.read()
data.replace('<MacELxsi:type="MatrixP" Field="NAME" Id="'+var1+'"> <Metadata Key="NAME" Value="'+var2+"/> </MacEL>', '<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>')
with open(i_path0, "w") as w:
w.write(data)
I am trying to replace this …
<MacELxsi:type="MatrixP" Field="NAME" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/> </MacEL>
<MacELxsi:type="MatrixP" Field="DESCN" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/> </MacEL>
<MacELxsi:type="MatrixP" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/></MacEL>
<MacELxsi:type="MatrixP" Field="UNIT" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/></MacEL>
<MacELxsi:type="MatrixP" Field="VAL" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/></MacEL>
<MacELxsi:type="MatrixP" Field="TEST" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/></MacEL>
<MacELxsi:type="MatrixP" Field="TIME" Id="9.ZZXX"> <Metadata Key="NAME" Value="ZZXX"/> </MacEL>
With this…
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
<MacELxsi:type="MatrixLab" Text="Blah"><BackgroundColor ColorCode="Grey"/> <ForegroundColor ColorCode="White"/></MacEL>
The list will contain mutliple values like this …testlist =[ZZXX, XXZZ, YYZZ, XXYY]
Any suggestions would be apprecaited.