434,905 Members | 2,064 Online
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

search and replace first amount of strings instances with one thing and a second amou

 P: 8 i have a code in python to search and replace what i need though is to replace the first say 10 instances of the number 1 with 2 and the second 10 instances with the number 3. anybody knows how to do that? fin = open(r'F:\1\xxx.txt') fout = open(r'F:\1\xxx2.txt', "wt") for line in fin: fout.write( line.replace('1', '2') ) fin.close() fout.close() Sep 22 '17 #1

solved by code

get_all_index = lambda s : [i for i,_ in enumerate(lines) if i-len(s)+1 >= 0 and lines[i-len(s)+1:i+1] == s]

with open(r'F:\1\xxx.txt') as fin:
# get a list of all instances of number 1
all_index = get_all_index('yyyy1')
# convert the file string into a list
list_lines = list(lines)
# replace the first 10 instances with 2;
# then the second 10 instances with 3
for i in all_index[:10]:
list_lines[i] = '2'
for i in all_index[10:20]:
list_lines[i] = '3'
# write the resulting string into output file
with open(r'F:\1\xxx2.txt','w') as fout:
fout.write(''.join(list_lines))

5 Replies

 Expert 100+ P: 621 Use a counter to count the number of replaces Expand|Select|Wrap|Line Numbers num_replaced=0 fin = open(r'F:\1\xxx.txt') fout = open(r'F:\1\xxx2.txt', "wt") replace_out="2" ctr=0 for line in fin:     fout.write( line.replace('1', replace_out) )     ctr += 1     if ctr > 9:         replace_out="3"         ctr=0 fin.close() fout.close()  Sep 23 '17 #2

 P: 8 didnt work for some reason. i tested putting in xxx.txt 1111111111 1111111111 and i got 2222222222 2222222222 in xxx2.txt Sep 24 '17 #3

 P: 8 so i was given the code with open(r'F:\1\xxx.txt') as fin: lines = fin.read() # get a list of all instances of number 1 all_index = [i for i,x in enumerate(lines) if i != 0 and lines[i-1:i+1] == 'x1'] # convert the file string into a list list_lines = list(lines) # replace the first 10 instances with 2; # then the second 10 instances with 3 for i in all_index[:10]: list_lines[i] = '2' for i in all_index[10:20]: list_lines[i] = '3' # write the resulting string into output file with open(r'F:\1\xxx2.txt','w') as fout: fout.write(''.join(list_lines)) and it works in replacing the string 'x1'. which almost gets me exactly what i need but doesn't work with longer strings like 'stop1'. does anybody know why? Sep 24 '17 #4

 Expert 100+ P: 621 I have no idea what you want to do, and "didn't work" provides no additional information. didnt work for some reason. i tested putting in xxx.txt 1111111111 1111111111 and i got 2222222222 2222222222 when you said replace the first say 10 instances of the number 1 with 2 Isn't that what it just did on each of these lines??? Sep 24 '17 #5

 P: 8 solved by code get_all_index = lambda s : [i for i,_ in enumerate(lines) if i-len(s)+1 >= 0 and lines[i-len(s)+1:i+1] == s] with open(r'F:\1\xxx.txt') as fin: lines = fin.read() # get a list of all instances of number 1 all_index = get_all_index('yyyy1') # convert the file string into a list list_lines = list(lines) # replace the first 10 instances with 2; # then the second 10 instances with 3 for i in all_index[:10]: list_lines[i] = '2' for i in all_index[10:20]: list_lines[i] = '3' # write the resulting string into output file with open(r'F:\1\xxx2.txt','w') as fout: fout.write(''.join(list_lines)) Sep 24 '17 #6