Python Forum
search and replace first amount of strings instances with one thing and a second amou
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search and replace first amount of strings instances with one thing and a second amou
#1
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()
Reply
#2
Use the third parameter to str.replace.

>>> help(str.replace)
Help on method_descriptor:

replace(...)
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
>>> line = "1_1__" * 20
>>> line
'1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__'
>>> line.replace("1", "2")
'2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__2_2__'
>>> line.replace("1", "2", 10)
'2_2__2_2__2_2__2_2__2_2__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__'
>>> line.replace("1", "2", 10).replace("1", "3", 10)
'2_2__2_2__2_2__2_2__2_2__3_3__3_3__3_3__3_3__3_3__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__1_1__'
Reply
#3
sorry. i couldn't quite understand how to use your answer
Reply
#4
so i have a code in python for search and replace.

fin = open(r'F:\1\xxx.txt')
fout = open(r'F:\1\xxx2.txt', "wt")
for line in fin:
fout.write( line.replace('foo', 'bar') )
fin.close()
fout.close()

and I was wondering if there was a way to tell it to replace anything between a certain set of markers. like anything between the number 1111 and 2222 if they appear in the read text file. replace the whole paragraph that is with another paragraph. my purpose for this is to be able to update code files more effectively. so say i have similar code files. and i updated a certain segment that appears in all of them. instead of going one By one editing i could write them in advance between markers like

1111

2222

3333

and then be able to update the paragraph in between. anyone has a clue how this can be done?
Reply
#5
You can use the index method of the string to find the locations of '1111' and '2222' in the text. You can then use slicing to pull out the text between them, and the replace method of that string to replace the text. You can then combine the text before '1111', the modified text, and the text starting with '2222' using any number of methods (+, join method, format method).

By code, do you mean a computer program? Because if you have the same code in multiple files you should really factor that code out into a function, and then call that function from all the places the code used to be. Then you only need to modify the code once, in the function. This sort of code reuse is done precisely to avoid the problem you are trying to solve.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
ichabod801 thanks for the reply! i know this is a lot to ask but any chance you could write an example code for me?. im just really new to python and coding in general. i would be greatly grateful
Reply
#7
Quote: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?
nilamo's answer basically gave you exactly what you requested. You just need to read the file content in as a string to use str.replace
Recommended Tutorials:
Reply
#8
think for all the help guys. found this software by the way which makes it really easy if its relevant to anyone- nodesoft-searchandreaplce
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 970 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Need help i just started the whole thing gabriel789 16 3,228 Sep-12-2022, 08:04 PM
Last Post: snippsat
  Replace for loop to search index position illmattic 5 1,284 Sep-03-2022, 04:04 PM
Last Post: illmattic
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,218 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,773 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 8,054 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  i making a terminal sign up website thing Kenrichppython 1 1,706 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  Get amount of bytes in a file chesschaser 1 1,582 Aug-23-2021, 03:24 PM
Last Post: deanhystad
  use thing before self sylvanas 3 2,350 Jul-21-2021, 04:54 PM
Last Post: sylvanas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020