0

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?

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by woooee
1

"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 by woooee: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.