Home > front end >  Find Text In XML File and Convert To Uppercase
Find Text In XML File and Convert To Uppercase

Time:05-13

I have XML files that come in that I need to change a field name to be all capital letters. I am trying to automate this in Python and have only been able to convert the entire line of the file to all caps.

Is there a way to only capitalize the text between:

<Index Name="NAME">Name to Capitalize</Index> 

to look like this in Python?:

<Index Name="NAME">NAME TO CAPITALIZE</Index>

I was told to try python to automate this. Currently we use a batch script or change the text manually. This is what I have so far.

import re

file_input  = 'index.XML'
file_output = 'output.XML'

with open(file_input) as file_object:
    for line in file_object:   

        # replace the effected line with all caps
        if re.search('<Index Name="NAME">', line): 
            line = line.upper().rstrip()
        line = line.rstrip()
        print(line)

        # write each line to a file
        file_output = open("output.XML", 'a')
        file_output.write(line   '\n')

CodePudding user response:

For the code in question, it can be changed as follows to get desired result. Or xmltree can be used as well -

import re
f=open(r'input.xml', "r")
f_out = open("output.XML", 'a')

newtag=''

while(True):
    line = f.readline()
    if  not line:
        break
    if re.search('<Index Name="NAME">', line): 
        newtag = line [:line.find('>') 1]   line[line.find('>') 1:line.find('</')].upper()   line[line.find('</'):]
        print (newtag, end="") ## Not needed for this code
    else:
        newtag = line
    f_out.write(newtag)

Will produce result as -

$ more input.xml
<Index Name="NAME">name to capitalize</Index>
<Index Name="NAME">name to capitalize</Index>
<Index Name="IDX1">name to capitalize</Index>
<Index Name="NAME">name to capitalize</Index>
<Index Name="IDX2">name to capitalize</Index>

$ python replace_in_file.py
<Index Name="NAME">NAME TO CAPITALIZE</Index>
<Index Name="NAME">NAME TO CAPITALIZE</Index>
<Index Name="NAME">NAME TO CAPITALIZE</Index>

$ more output.XML
<Index Name="NAME">NAME TO CAPITALIZE</Index>
<Index Name="NAME">NAME TO CAPITALIZE</Index>
<Index Name="IDX1">name to capitalize</Index>
<Index Name="NAME">NAME TO CAPITALIZE</Index>
<Index Name="IDX2">name to capitalize</Index>
  • Related