replacing string in xml file
Carsten Haese
carsten at uniqsys.com
Tue May 8 07:45:01 EDT 2007
On Tue, 08 May 2007 13:30:59 +0200, Stefan Behnel wrote
> saif.shakeel at gmail.com schrieb:
> [...]
> > file_input = raw_input("Enter The ODX File Path:")
> > input_xml = open(file_input,'r')
>
> This should say
>
> input_xml = open(file_input,'r').read()
...and then it still won't work because the OP's replace call assumes in-place
operation despite the fact that strings aren't mutable.
The OP needs something following this pattern:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()
For extra credit, use a different file name for writing out the result and
rename the file after it's written. That way you don't lose your file contents
if a meteor hits your CPU just after it started to overwrite the file.
Best regards,
--
Carsten Haese
http://informixdb.sourceforge.net
More information about the Python-list
mailing list