472,356 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

File I/O

Hi! I need some help in file I/O

I have an xml file..

<Top>
<Body1>xyz</Body1>
</Top>

I want to open these file in append mode and add the line
<Body2>abd</Body2>

Such that i have:

<Top>
<Body1>xyz</Body1>
<Body2>xyz</Body2>
</Top>

can anyone show me the way to achieve this??

thanx

Sep 29 '06 #1
10 1496

Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/

Sep 29 '06 #2

jimburton wrote:
Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/
i dont wanna parse the xml file..

Just open the file as:

f=open('test.xml','a')

and write a line "<Body2>abc</Body2>" before tag </Top?

Sep 29 '06 #3

jimburton wrote:
Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/
i dont wanna parse the xml file..

Just open the file as:

f=open('test.xml','a')

and append a line "<Body2>abc</Body2>" before tag </Top>

Sep 29 '06 #4

Kirt wrote:
i dont wanna parse the xml file..

Just open the file as:

f=open('test.xml','a')

and append a line "<Body2>abc</Body2>" before tag </Top>
Use a regex to split the contents and insert new stuff, eg

import re
prog = prog = re.compile('^(.*)(</Top>)', re.DOTALL)
m = prog.search(f.read())

then m.group(1) is everything before </Topand m.group(2) is </Top>.
Put them together with your new tag and write it back to the file

Sep 29 '06 #5
Kirt wrote:
>
jimburton wrote:
>Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/

i dont wanna parse the xml file..
If you play soccer, do you insist on playing with a baseball bat?

The game is called XML, and to play it, you should use a parser. If you
don't, you're up for trouble.

Diez
Sep 29 '06 #6

Diez B. Roggisch wrote:
Kirt wrote:

jimburton wrote:
Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/
i dont wanna parse the xml file..

If you play soccer, do you insist on playing with a baseball bat?
I've been known to open a bottle of wine with a pencil. Works fine.

Sep 29 '06 #7
jimburton wrote:
>
Diez B. Roggisch wrote:
>Kirt wrote:
>
jimburton wrote:
Kirt wrote:
Hi! I need some help in file I/O

I have an xml file..
[snip]
See http://diveintopython.org/xml_processing/

i dont wanna parse the xml file..

If you play soccer, do you insist on playing with a baseball bat?
I've been known to open a bottle of wine with a pencil. Works fine.
Good to know that you can get drunk under any circumstances.

I have seen a bazillion bad xml/html parsing-hacks using regexes and the
like, which stopped working after somehow the xml came all in one line, or
some other implicit assumption about its layout failed to be met.


Sep 29 '06 #8
Diez B. Roggisch wrote:
Good to know that you can get drunk under any circumstances.
+(++)1 heh!
I have seen a bazillion bad xml/html parsing-hacks using regexes and the
like, which stopped working after somehow the xml came all in one line, or
some other implicit assumption about its layout failed to be met.
Seriously, I fully agree with you. To manipulate X/HT/ML data reliably
you need to use a parser. And usually the parser API is much cleaner
and easier to work with than trying to cobble together regexps anyhow!

Regards,
Jordan

Sep 30 '06 #9
Ant

Kirt wrote:
....
i dont wanna parse the xml file..

Just open the file as:

f=open('test.xml','a')

and append a line "<Body2>abc</Body2>" before tag </Top>
The other guys are right - you should look at something like
ElementTree which makes this sort of thing pretty easy, and is robust.
But if you are sure that:

1) </Topis going to be on its own line (rather than something like
</Body1></Top>)
2) the ending tag will *definitely* have no extraneous whitespace (e.g.
< / Top >)

then the following will probably be the simplest solution:

f=open('test.xml')
out = []
for line in f:
if "</Top>" in line:
out.append("<Body2>abc</Body2>")
out.append(line")

f.close()

f_out = open("test.xml", "w")
f.write("".join(out))
f_out.close()

If you're working with XML in any sort of quantity, then look into the
available XML tools - it will be worth your time.

Sep 30 '06 #10
Ant wrote:
Kirt wrote:
...
i dont wanna parse the xml file..

Just open the file as:

f=open('test.xml','a')

and append a line "<Body2>abc</Body2>" before tag </Top>

The other guys are right - you should look at something like
ElementTree which makes this sort of thing pretty easy, and is robust.
But if you are sure that:

1) </Topis going to be on its own line (rather than something like
</Body1></Top>)
2) the ending tag will *definitely* have no extraneous whitespace (e.g.
< / Top >)

then the following will probably be the simplest solution:

f=open('test.xml')
out = []
for line in f:
if "</Top>" in line:
out.append("<Body2>abc</Body2>")
out.append(line")

f.close()

f_out = open("test.xml", "w")
f.write("".join(out))
f_out.close()
And the most dangerous solution. Start with the line
"out.append(line")"

And have a look at the many failure possibilities I detail here:

http://www.xml.com/pub/a/2002/11/13/py-xml.html

Then add to that the fact that "</Top>" can legitimately appear in an
XML comment, so that logic is even more brittle.

The following code does this *safely* with Amara:

import amara
doc = amara.parse('test.xml')
top = doc.xml_xpath('//Top')[0]
top.xml_parent.xml_insert_before(top, doc.xml_create_element(u'Body2',
content=u'abc'))
top.xml(stream=open('test.xml', 'w'))

Amara: http://uche.ogbuji.net/tech/4suite/amara/
--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 6 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.