File I/O

jimburton jimburton1 at gmail.com
Fri Sep 29 07:18:21 EDT 2006


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 </Top> and m.group(2) is </Top>.
Put them together with your new tag and write it back to the file




More information about the Python-list mailing list