By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

File I/O

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a

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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.