Trying to do a Find/Replace Across Several Lines in Several Text Files
I have several blocks of text that look like this:
steps:
- class: pipe.steps.extract.Extract
conf:
unzip_patterns:
- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
id: extract
- class: pipe.steps.validate.Validate
conf:
schema_def:
fields:
I want to replace this block of text with this:
global:
global:
schema_def:
fields:
The catch here is that the text crosses several lines in each text file. Maybe there is an easy workaround for this, not sure. More troublesome, is that is don't always have '- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
'. Sometimes the text is '- .*EstimatesDaily_RealEstate_Y.*_{FD_YYYYMMDD}.*
' or it could be '- .*EstimatesDaily_RealEstate_EAP_Nav.*_{FD_YYYYMMDD}.*
' One thng that is always the same in each block is that it starts with this ' steps:
' and ends with this ' fields:
'.
My sample code looks like this:
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
regex = re.compile("steps:.*fields:", re.DOTALL)
print(regex)
replace = """global:
global:
schema_def:
fields:"""
for fname in glob.glob(path):
#print(str(fname))
with open(fname, 'r+') as f:
text = re.sub(regex, replace, '')
f.seek(0)
f.write(text)
f.truncate()
Of course, my example isn't simple.
python python-3.x
add a comment |
I have several blocks of text that look like this:
steps:
- class: pipe.steps.extract.Extract
conf:
unzip_patterns:
- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
id: extract
- class: pipe.steps.validate.Validate
conf:
schema_def:
fields:
I want to replace this block of text with this:
global:
global:
schema_def:
fields:
The catch here is that the text crosses several lines in each text file. Maybe there is an easy workaround for this, not sure. More troublesome, is that is don't always have '- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
'. Sometimes the text is '- .*EstimatesDaily_RealEstate_Y.*_{FD_YYYYMMDD}.*
' or it could be '- .*EstimatesDaily_RealEstate_EAP_Nav.*_{FD_YYYYMMDD}.*
' One thng that is always the same in each block is that it starts with this ' steps:
' and ends with this ' fields:
'.
My sample code looks like this:
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
regex = re.compile("steps:.*fields:", re.DOTALL)
print(regex)
replace = """global:
global:
schema_def:
fields:"""
for fname in glob.glob(path):
#print(str(fname))
with open(fname, 'r+') as f:
text = re.sub(regex, replace, '')
f.seek(0)
f.write(text)
f.truncate()
Of course, my example isn't simple.
python python-3.x
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work
– Jean-François Fabre
Nov 14 '18 at 20:07
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
1
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
1
This looks like ayaml
file, is the entire file in the same format? If so maybe you just need to load theyaml
properly and change the keys/values, it'll be much simpler.
– Idlehands
Nov 14 '18 at 20:47
add a comment |
I have several blocks of text that look like this:
steps:
- class: pipe.steps.extract.Extract
conf:
unzip_patterns:
- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
id: extract
- class: pipe.steps.validate.Validate
conf:
schema_def:
fields:
I want to replace this block of text with this:
global:
global:
schema_def:
fields:
The catch here is that the text crosses several lines in each text file. Maybe there is an easy workaround for this, not sure. More troublesome, is that is don't always have '- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
'. Sometimes the text is '- .*EstimatesDaily_RealEstate_Y.*_{FD_YYYYMMDD}.*
' or it could be '- .*EstimatesDaily_RealEstate_EAP_Nav.*_{FD_YYYYMMDD}.*
' One thng that is always the same in each block is that it starts with this ' steps:
' and ends with this ' fields:
'.
My sample code looks like this:
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
regex = re.compile("steps:.*fields:", re.DOTALL)
print(regex)
replace = """global:
global:
schema_def:
fields:"""
for fname in glob.glob(path):
#print(str(fname))
with open(fname, 'r+') as f:
text = re.sub(regex, replace, '')
f.seek(0)
f.write(text)
f.truncate()
Of course, my example isn't simple.
python python-3.x
I have several blocks of text that look like this:
steps:
- class: pipe.steps.extract.Extract
conf:
unzip_patterns:
- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
id: extract
- class: pipe.steps.validate.Validate
conf:
schema_def:
fields:
I want to replace this block of text with this:
global:
global:
schema_def:
fields:
The catch here is that the text crosses several lines in each text file. Maybe there is an easy workaround for this, not sure. More troublesome, is that is don't always have '- .*EstimatesDaily_RealEstate_Q.*_{FD_YYYYMMDD}.*
'. Sometimes the text is '- .*EstimatesDaily_RealEstate_Y.*_{FD_YYYYMMDD}.*
' or it could be '- .*EstimatesDaily_RealEstate_EAP_Nav.*_{FD_YYYYMMDD}.*
' One thng that is always the same in each block is that it starts with this ' steps:
' and ends with this ' fields:
'.
My sample code looks like this:
import glob
import re
path = 'C:\Users\ryans\OneDrive\Desktop\output\*.yaml'
regex = re.compile("steps:.*fields:", re.DOTALL)
print(regex)
replace = """global:
global:
schema_def:
fields:"""
for fname in glob.glob(path):
#print(str(fname))
with open(fname, 'r+') as f:
text = re.sub(regex, replace, '')
f.seek(0)
f.write(text)
f.truncate()
Of course, my example isn't simple.
python python-3.x
python python-3.x
edited Feb 11 at 7:37
piet.t
10.1k73245
10.1k73245
asked Nov 14 '18 at 20:03
ryguy72ryguy72
4,3511820
4,3511820
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work
– Jean-François Fabre
Nov 14 '18 at 20:07
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
1
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
1
This looks like ayaml
file, is the entire file in the same format? If so maybe you just need to load theyaml
properly and change the keys/values, it'll be much simpler.
– Idlehands
Nov 14 '18 at 20:47
add a comment |
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work
– Jean-François Fabre
Nov 14 '18 at 20:07
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
1
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
1
This looks like ayaml
file, is the entire file in the same format? If so maybe you just need to load theyaml
properly and change the keys/values, it'll be much simpler.
– Idlehands
Nov 14 '18 at 20:47
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work– Jean-François Fabre
Nov 14 '18 at 20:07
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work– Jean-François Fabre
Nov 14 '18 at 20:07
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
1
1
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
1
1
This looks like a
yaml
file, is the entire file in the same format? If so maybe you just need to load the yaml
properly and change the keys/values, it'll be much simpler.– Idlehands
Nov 14 '18 at 20:47
This looks like a
yaml
file, is the entire file in the same format? If so maybe you just need to load the yaml
properly and change the keys/values, it'll be much simpler.– Idlehands
Nov 14 '18 at 20:47
add a comment |
2 Answers
2
active
oldest
votes
Since you're doing a general replacement of things between strings, I'd say this calls for a regular expression [EDIT: Sorry, I see you've since replaced your string "replace" statements with regexp code]. So if your file is "myfile.txt", try this:
>>> import re
>>> f = open('myfile.txt', 'r')
>>> content = f.read()
>>> f.close()
>>> replacement = ' global:n global:n schema_def:n fields:'
>>> print re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
The output here should be the original contents of "myfile.txt" with all of the substitutions.
Instead of editing files directly, the usual convention in Python is to just copy what you need from a file, change it, and write everything back to a new file. It's less error prone this way, and should be fine unless you're dealing with an astronomically huge amount of content. So you could replace the last line I have here with something like this:
>>> newcontent = re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
>>> f = open('newfile.txt', 'w')
>>> f.write(newcontent)
>>> f.close()
add a comment |
Regex is the best answer here probably. Will make this simple. Your mileage will vary with my example regex. Make it as tight as you need to make sure you only replace what you need to and dont get false positives.
import re
#re.DOTALL means it matches across newlines!
regex = re.compile("steps:.*?fields:", flags=re.DOTALL, count=1)
replace = """global:
global:
schema_def:
fields:"""
def do_replace(fname):
with open(fname, 'r') as f:
in = f.read()
with open(fname, 'w') as f:
f.write(re.sub(regex, replace, in))
for fname in glob.glob(path):
print(str(fname))
do_replace(fname)
This will replace everything between the firststep
to the very lastfield
. Possibly truncating the entire file to just"global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307961%2ftrying-to-do-a-find-replace-across-several-lines-in-several-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you're doing a general replacement of things between strings, I'd say this calls for a regular expression [EDIT: Sorry, I see you've since replaced your string "replace" statements with regexp code]. So if your file is "myfile.txt", try this:
>>> import re
>>> f = open('myfile.txt', 'r')
>>> content = f.read()
>>> f.close()
>>> replacement = ' global:n global:n schema_def:n fields:'
>>> print re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
The output here should be the original contents of "myfile.txt" with all of the substitutions.
Instead of editing files directly, the usual convention in Python is to just copy what you need from a file, change it, and write everything back to a new file. It's less error prone this way, and should be fine unless you're dealing with an astronomically huge amount of content. So you could replace the last line I have here with something like this:
>>> newcontent = re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
>>> f = open('newfile.txt', 'w')
>>> f.write(newcontent)
>>> f.close()
add a comment |
Since you're doing a general replacement of things between strings, I'd say this calls for a regular expression [EDIT: Sorry, I see you've since replaced your string "replace" statements with regexp code]. So if your file is "myfile.txt", try this:
>>> import re
>>> f = open('myfile.txt', 'r')
>>> content = f.read()
>>> f.close()
>>> replacement = ' global:n global:n schema_def:n fields:'
>>> print re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
The output here should be the original contents of "myfile.txt" with all of the substitutions.
Instead of editing files directly, the usual convention in Python is to just copy what you need from a file, change it, and write everything back to a new file. It's less error prone this way, and should be fine unless you're dealing with an astronomically huge amount of content. So you could replace the last line I have here with something like this:
>>> newcontent = re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
>>> f = open('newfile.txt', 'w')
>>> f.write(newcontent)
>>> f.close()
add a comment |
Since you're doing a general replacement of things between strings, I'd say this calls for a regular expression [EDIT: Sorry, I see you've since replaced your string "replace" statements with regexp code]. So if your file is "myfile.txt", try this:
>>> import re
>>> f = open('myfile.txt', 'r')
>>> content = f.read()
>>> f.close()
>>> replacement = ' global:n global:n schema_def:n fields:'
>>> print re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
The output here should be the original contents of "myfile.txt" with all of the substitutions.
Instead of editing files directly, the usual convention in Python is to just copy what you need from a file, change it, and write everything back to a new file. It's less error prone this way, and should be fine unless you're dealing with an astronomically huge amount of content. So you could replace the last line I have here with something like this:
>>> newcontent = re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
>>> f = open('newfile.txt', 'w')
>>> f.write(newcontent)
>>> f.close()
Since you're doing a general replacement of things between strings, I'd say this calls for a regular expression [EDIT: Sorry, I see you've since replaced your string "replace" statements with regexp code]. So if your file is "myfile.txt", try this:
>>> import re
>>> f = open('myfile.txt', 'r')
>>> content = f.read()
>>> f.close()
>>> replacement = ' global:n global:n schema_def:n fields:'
>>> print re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
The output here should be the original contents of "myfile.txt" with all of the substitutions.
Instead of editing files directly, the usual convention in Python is to just copy what you need from a file, change it, and write everything back to a new file. It's less error prone this way, and should be fine unless you're dealing with an astronomically huge amount of content. So you could replace the last line I have here with something like this:
>>> newcontent = re.sub(r"(ssteps:)(.*?)(sfields:)", replacement, content, flags=re.DOTALL)
>>> f = open('newfile.txt', 'w')
>>> f.write(newcontent)
>>> f.close()
edited Nov 14 '18 at 21:48
answered Nov 14 '18 at 21:29
Bill M.Bill M.
835112
835112
add a comment |
add a comment |
Regex is the best answer here probably. Will make this simple. Your mileage will vary with my example regex. Make it as tight as you need to make sure you only replace what you need to and dont get false positives.
import re
#re.DOTALL means it matches across newlines!
regex = re.compile("steps:.*?fields:", flags=re.DOTALL, count=1)
replace = """global:
global:
schema_def:
fields:"""
def do_replace(fname):
with open(fname, 'r') as f:
in = f.read()
with open(fname, 'w') as f:
f.write(re.sub(regex, replace, in))
for fname in glob.glob(path):
print(str(fname))
do_replace(fname)
This will replace everything between the firststep
to the very lastfield
. Possibly truncating the entire file to just"global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
|
show 2 more comments
Regex is the best answer here probably. Will make this simple. Your mileage will vary with my example regex. Make it as tight as you need to make sure you only replace what you need to and dont get false positives.
import re
#re.DOTALL means it matches across newlines!
regex = re.compile("steps:.*?fields:", flags=re.DOTALL, count=1)
replace = """global:
global:
schema_def:
fields:"""
def do_replace(fname):
with open(fname, 'r') as f:
in = f.read()
with open(fname, 'w') as f:
f.write(re.sub(regex, replace, in))
for fname in glob.glob(path):
print(str(fname))
do_replace(fname)
This will replace everything between the firststep
to the very lastfield
. Possibly truncating the entire file to just"global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
|
show 2 more comments
Regex is the best answer here probably. Will make this simple. Your mileage will vary with my example regex. Make it as tight as you need to make sure you only replace what you need to and dont get false positives.
import re
#re.DOTALL means it matches across newlines!
regex = re.compile("steps:.*?fields:", flags=re.DOTALL, count=1)
replace = """global:
global:
schema_def:
fields:"""
def do_replace(fname):
with open(fname, 'r') as f:
in = f.read()
with open(fname, 'w') as f:
f.write(re.sub(regex, replace, in))
for fname in glob.glob(path):
print(str(fname))
do_replace(fname)
Regex is the best answer here probably. Will make this simple. Your mileage will vary with my example regex. Make it as tight as you need to make sure you only replace what you need to and dont get false positives.
import re
#re.DOTALL means it matches across newlines!
regex = re.compile("steps:.*?fields:", flags=re.DOTALL, count=1)
replace = """global:
global:
schema_def:
fields:"""
def do_replace(fname):
with open(fname, 'r') as f:
in = f.read()
with open(fname, 'w') as f:
f.write(re.sub(regex, replace, in))
for fname in glob.glob(path):
print(str(fname))
do_replace(fname)
edited Nov 15 '18 at 15:22
answered Nov 14 '18 at 20:21
gbtimmongbtimmon
3,32911528
3,32911528
This will replace everything between the firststep
to the very lastfield
. Possibly truncating the entire file to just"global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
|
show 2 more comments
This will replace everything between the firststep
to the very lastfield
. Possibly truncating the entire file to just"global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
This will replace everything between the first
step
to the very last field
. Possibly truncating the entire file to just "global:... fields"
– Idlehands
Nov 14 '18 at 20:54
This will replace everything between the first
step
to the very last field
. Possibly truncating the entire file to just "global:... fields"
– Idlehands
Nov 14 '18 at 20:54
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
Here's an example: regex101.com/r/hrtO5N/1
– Idlehands
Nov 14 '18 at 21:01
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
I just updated my original code with your code; I incorporated your ideas into mine. When I run this all files are stripped of all text, so something is definitely off here, but I'm not sure what it is.
– ryguy72
Nov 14 '18 at 21:06
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
The 'steps:.*fields:' won't work here because it gets the first and last occurrence of both those. The thing is, I have many of those string patterns in my file, repeating over and over, so doing it that way strips out everything in between the first and last instance of those strings, but I need to preserve that stuff.
– ryguy72
Nov 14 '18 at 21:10
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
Oh, wait, this seems to work: "steps:.*?fields:" However, when I use that, it still truncates everything in all the files.
– ryguy72
Nov 14 '18 at 21:29
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307961%2ftrying-to-do-a-find-replace-across-several-lines-in-several-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
with open(str(fname), "w") as f:
that kills the contents of the file so it doesn't work– Jean-François Fabre
Nov 14 '18 at 20:07
I just made an update to that code.
– ryguy72
Nov 14 '18 at 20:13
1
given that you call f.read() opening in 'r+' is just wasted resources. better to open once in read and again in write if you are not really going to seek to read AND write.
– gbtimmon
Nov 14 '18 at 20:23
1
This looks like a
yaml
file, is the entire file in the same format? If so maybe you just need to load theyaml
properly and change the keys/values, it'll be much simpler.– Idlehands
Nov 14 '18 at 20:47