473,542 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1567

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.xm l','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.xm l','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.xm l','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.r ead())

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.xm l','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.xm l')
out = []
for line in f:
if "</Top>" in line:
out.append("<Bo dy2>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

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

Similar topics

2
3911
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 them a caption, storing the caption and filename in a text file. It's a bit buggy when removing the photos and captions from the file, and also in...
5
5437
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. I want to develop a webpage where people can send attachments that are stored on their local PC.
7
3521
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. How do i make the last pointer in the indirect sector that has another level of indirect pointer, and be defined recursively to support infinite...
3
26247
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
3914
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header,...
13
4285
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 that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I...
1
5361
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 completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried,...
3
3152
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 I find a list of all the types which can be returned? For example, if it is a Word document will it return "document/doc".
0
2542
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 datalist. When the next user selects trys to run the page, the page fails and I get a generic error message from the stack trace. I am assuming...
0
2019
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 said to me that they have no problem doing that, but the following project file is said to be corrupted while opening in the IDE, it is the project...
0
7397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7582
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7675
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5877
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3384
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3385
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.