![]() |
search and replace first amount of strings instances with one thing and a second amou - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: search and replace first amount of strings instances with one thing and a second amou (/thread-5212.html) |
search and replace first amount of strings instances with one thing and a second amou - chickflick91 - Sep-22-2017 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() RE: search and replace first amount of strings instances with one thing and a second amou - nilamo - Sep-22-2017 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__' RE: search and replace first amount of strings instances with one thing and a second amou - chickflick91 - Sep-24-2017 sorry. i couldn't quite understand how to use your answer search and replace in python. replacing content between markers - chickflick91 - Sep-25-2017 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? RE: search and replace first amount of strings instances with one thing and a second amou - ichabod801 - Sep-25-2017 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. RE: search and replace first amount of strings instances with one thing and a second amou - chickflick91 - Sep-26-2017 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 RE: search and replace first amount of strings instances with one thing and a second amou - metulburr - Sep-26-2017 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 RE: search and replace first amount of strings instances with one thing and a second amou - chickflick91 - Sep-26-2017 think for all the help guys. found this software by the way which makes it really easy if its relevant to anyone- nodesoft-searchandreaplce |