Re: replacing string in xml file--revisited
[EMAIL PROTECTED] wrote: > On May 10, 1:55 pm, [EMAIL PROTECTED] wrote: >> from elementtree import ElementTree as et > > which module should be imported for above to work,it says > ImportError: No module named elementtree > Thanks What about trying a web search engine to answer your own question? Usually much faster for this kind of request. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
On May 10, 4:21 am, [EMAIL PROTECTED] wrote: > On May 10, 1:55 pm, [EMAIL PROTECTED] wrote: > > > > > On May 10, 12:56 am, [EMAIL PROTECTED] wrote: > > > > Hi, > > > I need to replace a string in xml file with something else.Ex > > > > - > > > rate > > > rate > > > > > > > > > > > > - > > > > Here i have opened an xml > > > file(small part is pasted here).I want to replace the word 'localId' > > > with 'dataPackageID' wherever it comes in xml file.I have asked this > > > before and got a code: > > > 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() > > > > Although this works alone it is nto > > > working when i handle multiple file I/O.Is there a alternative to do > > > this.(maybe without read() operation) > > > Thanks > > > After reading your post again, this might be better: > > > #!/usr/bin/env python > > > from elementtree import ElementTree as et > > tree = et.parse("testxml.xml") > > > for t in tree.getiterator("SERVICEPARAMETER"): > > if t.get("Semantics") == "localId": > > t.set("Semantics", "dataPackageID") > > > tree.write("output.xml") > > > ~Sean- Hide quoted text - > > > - Show quoted text - > > which module should be imported for above to work,it says > ImportError: No module named elementtree > Thanks You can either 1) upgrade to python 2.5 which includes the elementtree module or 2) download and add the module to your current installation http://effbot.org/zone/element-index.htm ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
On May 10, 1:55 pm, [EMAIL PROTECTED] wrote: > On May 10, 12:56 am, [EMAIL PROTECTED] wrote: > > > > > > > Hi, > > I need to replace a string in xml file with something else.Ex > > > - > > rate > > rate > > > > > > > > - > > > Here i have opened an xml > > file(small part is pasted here).I want to replace the word 'localId' > > with 'dataPackageID' wherever it comes in xml file.I have asked this > > before and got a code: > > 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() > > > Although this works alone it is nto > > working when i handle multiple file I/O.Is there a alternative to do > > this.(maybe without read() operation) > > Thanks > > After reading your post again, this might be better: > > #!/usr/bin/env python > > from elementtree import ElementTree as et > tree = et.parse("testxml.xml") > > for t in tree.getiterator("SERVICEPARAMETER"): > if t.get("Semantics") == "localId": > t.set("Semantics", "dataPackageID") > > tree.write("output.xml") > > ~Sean- Hide quoted text - > > - Show quoted text - which module should be imported for above to work,it says ImportError: No module named elementtree Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
On May 10, 1:42 pm, [EMAIL PROTECTED] wrote: > On May 10, 12:56 am, [EMAIL PROTECTED] wrote: > > > > > > > Hi, > > I need to replace a string in xml file with something else.Ex > > > - > > rate > > rate > > > > > > > > - > > > Here i have opened an xml > > file(small part is pasted here).I want to replace the word 'localId' > > with 'dataPackageID' wherever it comes in xml file.I have asked this > > before and got a code: > > 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() > > > Although this works alone it is nto > > working when i handle multiple file I/O.Is there a alternative to do > > this.(maybe without read() operation) > > Thanks > > try this... > > #!/usr/bin/env python > > from elementtree import ElementTree as et > tree = et.parse("testxml.xml") > > for t in tree.getiterator("SERVICEPARAMETER"): > t.set("Semantics", "localId") > > tree.write("output.xml") > > ~Sean- Hide quoted text - > > - Show quoted text - #!/usr/bin/env python from elementtree import ElementTree as et tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): t.set("Semantics", "localId") tree.write("output.xml") Is this code complete,where are you replacing the localid with "datapackageid",and where is the new xml being stored. Thanks for the replies -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
On May 10, 12:56 am, [EMAIL PROTECTED] wrote: > Hi, > I need to replace a string in xml file with something else.Ex > > - > rate > rate > > > > - > > Here i have opened an xml > file(small part is pasted here).I want to replace the word 'localId' > with 'dataPackageID' wherever it comes in xml file.I have asked this > before and got a code: > 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() > > Although this works alone it is nto > working when i handle multiple file I/O.Is there a alternative to do > this.(maybe without read() operation) > Thanks After reading your post again, this might be better: #!/usr/bin/env python from elementtree import ElementTree as et tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): if t.get("Semantics") == "localId": t.set("Semantics", "dataPackageID") tree.write("output.xml") ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
On May 10, 12:56 am, [EMAIL PROTECTED] wrote: > Hi, > I need to replace a string in xml file with something else.Ex > > - > rate > rate > > > > - > > Here i have opened an xml > file(small part is pasted here).I want to replace the word 'localId' > with 'dataPackageID' wherever it comes in xml file.I have asked this > before and got a code: > 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() > > Although this works alone it is nto > working when i handle multiple file I/O.Is there a alternative to do > this.(maybe without read() operation) > Thanks try this... #!/usr/bin/env python from elementtree import ElementTree as et tree = et.parse("testxml.xml") for t in tree.getiterator("SERVICEPARAMETER"): t.set("Semantics", "localId") tree.write("output.xml") ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file--revisited
In <[EMAIL PROTECTED]>, saif.shakeel wrote: > Although this works alone it is nto > working when i handle multiple file I/O.Is there a alternative to do > this.(maybe without read() operation) Why do you want to change the part that *works* instead of fixing the code that doesn't!? Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
replacing string in xml file--revisited
Hi, I need to replace a string in xml file with something else.Ex - rate rate - Here i have opened an xml file(small part is pasted here).I want to replace the word 'localId' with 'dataPackageID' wherever it comes in xml file.I have asked this before and got a code: 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() Although this works alone it is nto working when i handle multiple file I/O.Is there a alternative to do this.(maybe without read() operation) Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
En Thu, 10 May 2007 01:55:25 -0300, <[EMAIL PROTECTED]> escribió: >> I am opening 2 more files in addition to the file >> where the new xml goes.One file is written using the sys.stdout >> command as most of the output has to go there printing takes place in >> many places (so cant use f_open.write) each time. >> When i attach the part of replacing the string >> 'localid' in xml file with something else as given above with >> xmlcont=xmlcont.replace('localId','dataPackageId') >> the code does not run and hangs. What do you mean by "hangs"? You have replaced stdout - what if the program is prompting the user for something, and you can't see it? Can you type some text and continue? >> Can more than 3 files be opened at a >> time Sure. The limit actually depends on the OS, but on "normal" circumstances it's far above 3 files. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
On May 9, 4:39 pm, [EMAIL PROTECTED] wrote: > On May 8, 4:46 pm, [EMAIL PROTECTED] wrote: > > > > > > > On May 8, 4:30 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote: > > > > [EMAIL PROTECTED] schrieb: > > > > > Hi, > > > > I need to replace a string in xml file with something else.Ex > > > > > - > > > > rate > > > > rate > > > > > > > > > > > > > > > > - > > > > > Here i have opened an xml > > > > file(small part is pasted here).I want to replace the word 'localId' > > > > with 'dataPackageID' wherever it comes in xml file.I tried this but > > > > didnt work: > > > > > import sys > > > > > 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() > > > > > input_xml.replace('localId','dataPackageId') > > > > This gives error ---> AttributeError: 'file' > > > > object has no attribute 'replace' > > > > Can someone help me . > > > > Thanks > > > > Stefan- Hide quoted text - > > > > - Show quoted text - > > > There is no error now,but the string is not being replaced,It remains > > the same,should we save the opened file or something- Hide quoted text - > > > - Show quoted text - > > HI, > Thanks for the reply.that seems to work,but i was doing this > so as to attach it to a bigger code where it will be utilised before a > parsing. > > #Input file and Output file path from user > > file_input = raw_input("Enter The ODX File Path:") > > (shortname,ext)=os.path.splitext(file_input) > f_open_out=shortname+".ini" > log=shortname+".xls" > test_file=shortname+"testxml.xml" > > saveout = sys.stdout > > input_xml = open(file_input,'r') > xmlcont=input_xml.read() > xmlcont=xmlcont.replace('localId','dataPackageId') > output_file = open(test_file,"w") > output_file.write(xmlcont) > output_file.close() > > f_open=open(f_open_out, 'w') > logfile=open(log,"w") > > sys.stdout = f_open > > #Parse the input file,and check for correct ODX version > > xmldoc = minidom.parse(input_xml) > > I am opening 2 more files in addition to the file > where the new xml goes.One file is written using the sys.stdout > command as most of the output has to go there printing takes place in > many places (so cant use f_open.write) each time. > When i attach the part of replacing the string > 'localid' in xml file with something else as given above with > xmlcont=xmlcont.replace('localId','dataPackageId') > the code does not run and hangs.Can more than 3 files be opened at a > time .I dotn know what the problem is here. > Thanks- Hide quoted text - > > - Show quoted text - Hi, Cna someone help me in this ,or throw some insight . Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
On May 8, 4:46 pm, [EMAIL PROTECTED] wrote: > On May 8, 4:30 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote: > > > > > > > [EMAIL PROTECTED] schrieb: > > > > Hi, > > > I need to replace a string in xml file with something else.Ex > > > > - > > > rate > > > rate > > > > > > > > > > > > - > > > > Here i have opened an xml > > > file(small part is pasted here).I want to replace the word 'localId' > > > with 'dataPackageID' wherever it comes in xml file.I tried this but > > > didnt work: > > > > import sys > > > > 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() > > > > input_xml.replace('localId','dataPackageId') > > > This gives error ---> AttributeError: 'file' > > > object has no attribute 'replace' > > > Can someone help me . > > > Thanks > > > Stefan- Hide quoted text - > > > - Show quoted text - > > There is no error now,but the string is not being replaced,It remains > the same,should we save the opened file or something- Hide quoted text - > > - Show quoted text - HI, Thanks for the reply.that seems to work,but i was doing this so as to attach it to a bigger code where it will be utilised before a parsing. #Input file and Output file path from user file_input = raw_input("Enter The ODX File Path:") (shortname,ext)=os.path.splitext(file_input) f_open_out=shortname+".ini" log=shortname+".xls" test_file=shortname+"testxml.xml" saveout = sys.stdout input_xml = open(file_input,'r') xmlcont=input_xml.read() xmlcont=xmlcont.replace('localId','dataPackageId') output_file = open(test_file,"w") output_file.write(xmlcont) output_file.close() f_open=open(f_open_out, 'w') logfile=open(log,"w") sys.stdout = f_open #Parse the input file,and check for correct ODX version xmldoc = minidom.parse(input_xml) I am opening 2 more files in addition to the file where the new xml goes.One file is written using the sys.stdout command as most of the output has to go there printing takes place in many places (so cant use f_open.write) each time. When i attach the part of replacing the string 'localid' in xml file with something else as given above with xmlcont=xmlcont.replace('localId','dataPackageId') the code does not run and hangs.Can more than 3 files be opened at a time .I dotn know what the problem is here. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
On May 8, 4:30 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] schrieb: > > > > > > > Hi, > > I need to replace a string in xml file with something else.Ex > > > - > > rate > > rate > > > > > > > > - > > > Here i have opened an xml > > file(small part is pasted here).I want to replace the word 'localId' > > with 'dataPackageID' wherever it comes in xml file.I tried this but > > didnt work: > > > import sys > > > 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() > > > input_xml.replace('localId','dataPackageId') > > This gives error ---> AttributeError: 'file' > > object has no attribute 'replace' > > Can someone help me . > > Thanks > > Stefan- Hide quoted text - > > - Show quoted text - There is no error now,but the string is not being replaced,It remains the same,should we save the opened file or something -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
On Tue, 08 May 2007 13:30:59 +0200, Stefan Behnel wrote > [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
[EMAIL PROTECTED] schrieb: > Hi, > I need to replace a string in xml file with something else.Ex > > - > rate > rate > > > > - > > Here i have opened an xml > file(small part is pasted here).I want to replace the word 'localId' > with 'dataPackageID' wherever it comes in xml file.I tried this but > didnt work: > > import sys > > 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() > input_xml.replace('localId','dataPackageId') > This gives error ---> AttributeError: 'file' > object has no attribute 'replace' > Can someone help me . > Thanks > Stefan -- http://mail.python.org/mailman/listinfo/python-list
replacing string in xml file
Hi, I need to replace a string in xml file with something else.Ex - rate rate - Here i have opened an xml file(small part is pasted here).I want to replace the word 'localId' with 'dataPackageID' wherever it comes in xml file.I tried this but didnt work: import sys file_input = raw_input("Enter The ODX File Path:") input_xml = open(file_input,'r') input_xml.replace('localId','dataPackageId') This gives error ---> AttributeError: 'file' object has no attribute 'replace' Can someone help me . Thanks -- http://mail.python.org/mailman/listinfo/python-list