How would I iterate onto the next line of a file within a nested loop using “for line” in fileinput?












1















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.










share|improve this question

























  • 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













  • 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
















1















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.










share|improve this question

























  • 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













  • 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














1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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











  • 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

















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












1 Answer
1






active

oldest

votes


















3














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






share|improve this answer


























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











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


}
});














draft saved

draft discarded


















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









3














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






share|improve this answer


























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
















3














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






share|improve this answer


























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














3












3








3







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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



















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

















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas