How would I iterate onto the next line of a file within a nested loop using “for line” in fileinput?
I have eliminated some of the nested loops for simplicity of the example.
I am iterating over a file line-by-line using fileinput
. If the line meets a certain condition I want it to replace all future lines with '' until it meets the condition again.
import re
import fileinput
with fileinput.FileInput("survey.qsf", inplace=True, backup='.bak') as file:
for line in file:
if re.match(r'l'+datamap[i][2]+'.*$',line)!=None:
line=re.sub(r'.*$','',line)
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
next(line)
I used "next(line)" as a placeholder as I can't figure out how to iterate to the next line without breaking out of the inner loop.
I want to be able to iterate through the lines to have:
lQID44
xyz
xyz
lQID45
output as:
[blank line]
[blank line]
[blank line]
lQID45
Thanks.
python python-3.x file-io
add a comment |
I have eliminated some of the nested loops for simplicity of the example.
I am iterating over a file line-by-line using fileinput
. If the line meets a certain condition I want it to replace all future lines with '' until it meets the condition again.
import re
import fileinput
with fileinput.FileInput("survey.qsf", inplace=True, backup='.bak') as file:
for line in file:
if re.match(r'l'+datamap[i][2]+'.*$',line)!=None:
line=re.sub(r'.*$','',line)
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
next(line)
I used "next(line)" as a placeholder as I can't figure out how to iterate to the next line without breaking out of the inner loop.
I want to be able to iterate through the lines to have:
lQID44
xyz
xyz
lQID45
output as:
[blank line]
[blank line]
[blank line]
lQID45
Thanks.
python python-3.x file-io
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd usestr.startswith
rather thanre
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....
– SpghttCd
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to aStopIteration
exception, if there is no next line, so maybe you should guard your call ofnext
with a try-except-block, if you cannot make sure that there will always be a next line.
– Jan Christoph Terasa
Jan 2 at 14:55
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15
add a comment |
I have eliminated some of the nested loops for simplicity of the example.
I am iterating over a file line-by-line using fileinput
. If the line meets a certain condition I want it to replace all future lines with '' until it meets the condition again.
import re
import fileinput
with fileinput.FileInput("survey.qsf", inplace=True, backup='.bak') as file:
for line in file:
if re.match(r'l'+datamap[i][2]+'.*$',line)!=None:
line=re.sub(r'.*$','',line)
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
next(line)
I used "next(line)" as a placeholder as I can't figure out how to iterate to the next line without breaking out of the inner loop.
I want to be able to iterate through the lines to have:
lQID44
xyz
xyz
lQID45
output as:
[blank line]
[blank line]
[blank line]
lQID45
Thanks.
python python-3.x file-io
I have eliminated some of the nested loops for simplicity of the example.
I am iterating over a file line-by-line using fileinput
. If the line meets a certain condition I want it to replace all future lines with '' until it meets the condition again.
import re
import fileinput
with fileinput.FileInput("survey.qsf", inplace=True, backup='.bak') as file:
for line in file:
if re.match(r'l'+datamap[i][2]+'.*$',line)!=None:
line=re.sub(r'.*$','',line)
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
next(line)
I used "next(line)" as a placeholder as I can't figure out how to iterate to the next line without breaking out of the inner loop.
I want to be able to iterate through the lines to have:
lQID44
xyz
xyz
lQID45
output as:
[blank line]
[blank line]
[blank line]
lQID45
Thanks.
python python-3.x file-io
python python-3.x file-io
edited Jan 2 at 15:06
Moshe Slavin
2,41441026
2,41441026
asked Jan 2 at 14:48
Tom MurleyTom Murley
84
84
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd usestr.startswith
rather thanre
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....
– SpghttCd
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to aStopIteration
exception, if there is no next line, so maybe you should guard your call ofnext
with a try-except-block, if you cannot make sure that there will always be a next line.
– Jan Christoph Terasa
Jan 2 at 14:55
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15
add a comment |
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd usestr.startswith
rather thanre
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....
– SpghttCd
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to aStopIteration
exception, if there is no next line, so maybe you should guard your call ofnext
with a try-except-block, if you cannot make sure that there will always be a next line.
– Jan Christoph Terasa
Jan 2 at 14:55
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd use
str.startswith
rather than re
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....– SpghttCd
Jan 2 at 14:55
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd use
str.startswith
rather than re
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....– SpghttCd
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to a StopIteration
exception, if there is no next line, so maybe you should guard your call of next
with a try-except-block, if you cannot make sure that there will always be a next line.– Jan Christoph Terasa
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to a StopIteration
exception, if there is no next line, so maybe you should guard your call of next
with a try-except-block, if you cannot make sure that there will always be a next line.– Jan Christoph Terasa
Jan 2 at 14:55
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15
add a comment |
1 Answer
1
active
oldest
votes
next
takes the iterator as its argument.
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
try:
line = next(file) # Not next(line)
except StopIteration:
break
As an aside, there's no need to use re.sub
to replace the entire line with an empty string; line = ''
would suffice.
(Also, assigning to line
doesn't make changes to the actual file; inplace=True
just means that you can write to file
as well as read from it, but you have to explicitly write to the file, using print
or file.write
.)
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
I forgot to mention that you need to reassign its return value toline
so that you are checking the new line, rather than the same previous line over and over again. But it is true that callingnext
explicitly requires you to catchStopIteration
your self when you do reach the end of the file, unlike thefor
loop which catches it for you (in addition to callingnext
for you implicitly).
– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
add a comment |
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%2f54008379%2fhow-would-i-iterate-onto-the-next-line-of-a-file-within-a-nested-loop-using-for%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
next
takes the iterator as its argument.
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
try:
line = next(file) # Not next(line)
except StopIteration:
break
As an aside, there's no need to use re.sub
to replace the entire line with an empty string; line = ''
would suffice.
(Also, assigning to line
doesn't make changes to the actual file; inplace=True
just means that you can write to file
as well as read from it, but you have to explicitly write to the file, using print
or file.write
.)
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
I forgot to mention that you need to reassign its return value toline
so that you are checking the new line, rather than the same previous line over and over again. But it is true that callingnext
explicitly requires you to catchStopIteration
your self when you do reach the end of the file, unlike thefor
loop which catches it for you (in addition to callingnext
for you implicitly).
– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
add a comment |
next
takes the iterator as its argument.
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
try:
line = next(file) # Not next(line)
except StopIteration:
break
As an aside, there's no need to use re.sub
to replace the entire line with an empty string; line = ''
would suffice.
(Also, assigning to line
doesn't make changes to the actual file; inplace=True
just means that you can write to file
as well as read from it, but you have to explicitly write to the file, using print
or file.write
.)
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
I forgot to mention that you need to reassign its return value toline
so that you are checking the new line, rather than the same previous line over and over again. But it is true that callingnext
explicitly requires you to catchStopIteration
your self when you do reach the end of the file, unlike thefor
loop which catches it for you (in addition to callingnext
for you implicitly).
– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
add a comment |
next
takes the iterator as its argument.
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
try:
line = next(file) # Not next(line)
except StopIteration:
break
As an aside, there's no need to use re.sub
to replace the entire line with an empty string; line = ''
would suffice.
(Also, assigning to line
doesn't make changes to the actual file; inplace=True
just means that you can write to file
as well as read from it, but you have to explicitly write to the file, using print
or file.write
.)
next
takes the iterator as its argument.
while re.match(r'lQIDd*$',line)==None:
line=re.sub(r'.*$','',line)
try:
line = next(file) # Not next(line)
except StopIteration:
break
As an aside, there's no need to use re.sub
to replace the entire line with an empty string; line = ''
would suffice.
(Also, assigning to line
doesn't make changes to the actual file; inplace=True
just means that you can write to file
as well as read from it, but you have to explicitly write to the file, using print
or file.write
.)
edited Jan 2 at 15:10
answered Jan 2 at 14:54
chepnerchepner
256k34246337
256k34246337
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
I forgot to mention that you need to reassign its return value toline
so that you are checking the new line, rather than the same previous line over and over again. But it is true that callingnext
explicitly requires you to catchStopIteration
your self when you do reach the end of the file, unlike thefor
loop which catches it for you (in addition to callingnext
for you implicitly).
– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
add a comment |
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
I forgot to mention that you need to reassign its return value toline
so that you are checking the new line, rather than the same previous line over and over again. But it is true that callingnext
explicitly requires you to catchStopIteration
your self when you do reach the end of the file, unlike thefor
loop which catches it for you (in addition to callingnext
for you implicitly).
– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
Hi, thanks for that. I think that "next()" might have a slight issue iterating fileinput because I get a StopIIteration error even though it isn't the last line when next(file) is used. Thanks for the other explanations though as this project is bigger than I had anticipated and my programming needs some finesse!
– Tom Murley
Jan 2 at 15:09
1
1
I forgot to mention that you need to reassign its return value to
line
so that you are checking the new line, rather than the same previous line over and over again. But it is true that calling next
explicitly requires you to catch StopIteration
your self when you do reach the end of the file, unlike the for
loop which catches it for you (in addition to calling next
for you implicitly).– chepner
Jan 2 at 15:10
I forgot to mention that you need to reassign its return value to
line
so that you are checking the new line, rather than the same previous line over and over again. But it is true that calling next
explicitly requires you to catch StopIteration
your self when you do reach the end of the file, unlike the for
loop which catches it for you (in addition to calling next
for you implicitly).– chepner
Jan 2 at 15:10
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
Brilliant, thanks again mate. Thanks for simplifying my code whilst you were at it!
– Tom Murley
Jan 2 at 15:16
add a comment |
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%2f54008379%2fhow-would-i-iterate-onto-the-next-line-of-a-file-within-a-nested-loop-using-for%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
1st: i'd simply set a boolean variable to true or false when matching occurs and replace lines dependent on this variable. No nested loops. 2nd: i'd use
str.startswith
rather thanre
, as imo that would suit here too. However, perhaps i just didn't get the complicated point here....– SpghttCd
Jan 2 at 14:55
next(file)
gives you the next line of the iterator. Beware that using it like this can lead to aStopIteration
exception, if there is no next line, so maybe you should guard your call ofnext
with a try-except-block, if you cannot make sure that there will always be a next line.– Jan Christoph Terasa
Jan 2 at 14:55
I didn't know about str.startswith I just got into the habit of using re. Yeah I am finding that iterating with next(file) leads to a StopIteration exception I'll catch that and then it should work. Thanks all.
– Tom Murley
Jan 2 at 15:15