473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1562

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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.