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

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


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.



More information about the Python-list mailing list