python - Finding and replacing in text file -


i have list of integers looks like:

i = [1020 1022 .... ]

i need open xml file stored .txt , each entry includes

settings="keys1029"/> 

i need iterate through records replacing each numbers in "keys1029" list entry. instead of having:

....settings="keys1029"/> ....settings="keys1029"/> 

we have:

....settings="keys1020"/> ....settings="keys1022"/> 

so far have:

out =   [1020 1022 .... ] text = open('c:\xml1.txt','r')  item in out:     text.replace('1029', item) 

but i'm getting:

text.replace('1029', item) attributeerror: 'file' object has no attribute 'replace' 

could advise me on how fix this?

thank you,

bill

open() returns file object can't use string operations on it, you've use either readlines() or read() text file object.

import os out =   [1020,1022] open('c:\xml1.txt') f1,open('c:\somefile.txt',"w") f2:     #somefile.txt temporary file     text = f1.read()     item in out:         text = text.replace("1029",str(item),1)     f2.write(text) #rename temporary file real file os.rename('c:\somefile.txt','c:\xml1.txt') 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -