Re: replacing string in xml file



On Tue, 08 May 2007 13:30:59 +0200, Stefan Behnel wrote
saif.shakeel@xxxxxxxxx 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
.



Relevant Pages

  • Re: [PATCH] Fix CFLAGS overwrite by Makefile
    ... Many Makefile overwrite the value of CFLAGS. ... CFLAGS might already have been set with CPU ... you NEVER want any CPU specific settings! ...
    (freebsd-current)
  • Re: Error signature
    ... clt wrote: ... CPU Feature Code: BFEBFBFF ...
    (microsoft.public.windows.mediacenter)
  • Re: Emulator and device problems with CF 2.0 and Visual Studio 200
    ... I doubt CPU ID change has anything to do with connection problem. ... > I manually copied the CF 2.0 CAB file to the ... >> Best regards, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: CPU load problem...
    ... >> CPU: Intel Xeon MP 2.2GHz, ... >> Best Regards, ... >> W. Jordan ... >>>I need to know the profile (resolution, input device, frame rate bit ...
    (microsoft.public.windowsmedia.encoder)
  • Re: Double-Checked Locking pattern issue
    ... It is my mistake before to think reorder of assignment and constructor is ... some specific CPU. ...
    (microsoft.public.vc.language)