replace - Python print full text file -
i want replace word "example" on textfile2.txt list of words textfile1.txt until list runs out or "example" have been replaced want display whole finished text.
how this?
textfile1.txt
user1 user2
textfile2.txt
url goto=https://www.url.com/example tag pos=1 type=button attr=txt:follow url goto=https://www.url.com/example tag pos=1 type=button attr=txt:follow
current code:
with open('textfile1.txt') f1, open('textfile2.txt') f2: l, r in zip(f1, f2): print(r[:r.find('/example') + 1] + l)
results gives me:
url goto=https://www.instagram.com/user1 user2
goal:
url goto=https://www.url.com/user1 tag pos=1 type=button attr=txt:follow url goto=https://www.url.com/user2 tag pos=1 type=button attr=txt:follow
here solution:
with open('t1.txt') f1, open('t2.txt') f2: url_info = f2.read().split('\n\n') users = f1.read().split('\n') zipped_list = zip(users, url_info) item in zipped_list: print item[1].replace('example', item[0])+"\n"
updated: need import itertools
import itertools open('t1.txt') f1, open('t2.txt') f2: url_info = f2.read().split('\n\n') users = [u u in f1.read().split('\n') if u] zipped_list = list(itertools.izip(url_info, itertools.cycle(users))) item in zipped_list: print item[0].replace('example', item[1])+"\n"
output:
url goto=https://www.url.com/user1 tag pos=1 type=button attr=txt:follow url goto=https://www.url.com/user2 tag pos=1 type=button attr=txt:follow url goto=https://www.url.com/user1 tag pos=1 type=button attr=txt:follow
Comments
Post a Comment