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

A

awel

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):
lines = lines.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
 
O

odeits

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):
        lines = lines.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.
 
O

odeits

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):
        lines = lines.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,727
Latest member
PasqualePf

Latest Threads

Top