I am trying to replace a fixed field name in an XML file with data. I know that I could do it the proper way using XML tools, but, this is a standard templae file that only needs one item replaced. It seems simpler to just hack a replacment.

x = open('C:\\test.xml','r')
    for row in x:
        print row
        if 'RecCount' in row:
            row.replace('RecCount','12345')
            print row

I would think this would work, but for some reason it's not. Isn't the inbound row a text line in using an Open?

"row" is a string, and strings are immutable, i.e. can not be changed, so you have to catch the return and use it.

for row in open('C:\\test.xml','r'):
    if 'RecCount' in row:
        print row
        new_row = row.replace('RecCount','12345')
        print new_row

Edited 5 Years Ago by woooee: n/a

That is one extra copy, but only matters if there is a lot of data.

if 'RecCount' in row:
        new_row = row.replace('RecCount','12345')
        output.write(new_row)
    else:
        output.write(row)
This question has already been answered. Start a new discussion instead.