How to replace the two last digits from an xml file?

odeits odeits at gmail.com
Fri Mar 6 18:37:22 EST 2009


On Mar 6, 3:31 pm, odeits <ode... at gmail.com> wrote:
> On Mar 6, 11:53 am, awel <albert.well... at gmail.com> wrote:
>
>
>
> > Hi,
>
> > I am trying to get a value from an xml file, increment it by 1,
> > replace by the new value and write another xml with the new value
> > inside.
>
> > I have found this code to get the line and it works but I have to do
> > everything manualy:
>
> > import re
> > lines = open("c:\\File1.xml").readlines()
> > for i in range(len(lines)):
> >     if re.search('national.number', lines[i]):
> >         lines[i] = lines[i].replace
> > ('01aaa0000000002','01aaa0000000003')
> > open("File2.xml","w").writelines(lines)
>
> > The goal I am trying to reach is to create 30 different xml.
>
> > Could someone help me?
>
> > Thanks
> > Al
>
> I would look into actually using an xml parser like lxml or the built
> in elementTree. You might run into some problems later on with the
> regex matching something unintended. Having said that..
>
> If you want to stick to regex i would pull out the number using a
> group, convert the number to a python type, increment then do the
> replace.

having dug a little deeper myself into the regular expressions in
python and grouping i stumbled onto this
http://www.amk.ca/python/howto/regex/regex.html#SECTION000520000000000000000

Right above section 6. Common Problems there is an example of how to
change decimal numbers to hex... with a little tweek i believe it will
do exactly what you want.

Good Luck



More information about the Python-list mailing list