473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

✓ answered by chickflick91

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))

5 1988
dwblas
626 Expert 512MB
Use a counter to count the number of replaces
Expand|Select|Wrap|Line Numbers
  1. num_replaced=0
  2. fin = open(r'F:\1\xxx.txt')
  3. fout = open(r'F:\1\xxx2.txt', "wt")
  4. replace_out="2"
  5. ctr=0
  6. for line in fin:
  7.     fout.write( line.replace('1', replace_out) )
  8.     ctr += 1
  9.     if ctr > 9:
  10.         replace_out="3"
  11.         ctr=0
  12. fin.close()
  13. fout.close() 
Sep 23 '17 #2
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
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
dwblas
626 Expert 512MB
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
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

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vamsee Krishna Gomatam | last post by:
Hello, I'm having some problems understanding Regexps in Python. I want to replace "<google>PHRASE</google>" with "<a href=http://www.google.com/search?q=PHRASE>PHRASE</a>" in a block of text....
0
by: MDW | last post by:
Mystring = Left(Mystring,Len(Mystring) - 1) Mystring = Mid(Mystring,2) That should do it. >-----Original Message----- >How can I replace first as well as last "," in a string? > >Mystring =...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: DaveJG | last post by:
I have a couple of questions involving the search/replace in the IDE and regular expressions. Any help would be greatly appreciated. 1. How would I replace two line feeds with one? That is ...
23
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
5
by: Wasim Akram | last post by:
Hi all, String.Replace replaces all occurrances of a substring in a given string . Can anyone guide me how can I replace first occurrance of a substring in a given string. - wa
2
by: Michael Peters | last post by:
is there a way to replace a certain sequence of characters by line feed (vbCrLf ), for all text columns in a table, using Search+Replace? -Michael
2
by: ameesha | last post by:
hi all.. Can any one help me.. i am asked to develop a software in which one option is to search the first occurence of a particular word within a text file..and save the word and preceeding...
0
by: kkshansid | last post by:
I have to work with result database with three tables first, second and third year each consisting of around 150 fields I have to make combined report of all three years but to show subject field of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.