Next Line Python File

04.02.2020
Next Line Python File Average ratng: 5,0/5 4188 votes
  1. New Line Python File

Hi,I am new to python and i wrote this piece of code which is ofcourse not serving my purpose:Aim of the code:To read a file and look for lines which contain the string 'CL'. On Sat, Sep 12, 2009 at 10:35 AM, ranjan das wrote: Hi, I am new to python and i wrote this piece of code which is ofcourse not serving my purpose: Aim of the code: To read a file and look for lines which contain the string 'CL'.

When found, print the entry of the next line (positioned directly below the string 'CL').continue to do this till the end of the file (since there are more than one occurrences of 'CL' in the file) My piece of code (which just prints lines which contain the string 'CL') f=open('somefile.txt','r') for line in f.readlines: if 'CL' in line: print line please suggest how do i print the entry right below the string 'CL'I would this using a boolean variable to denote whether the lineshould be printed:printline = false. Hi, I am new to python and i wrote this piece of code which is ofcourse not serving my purpose: Aim of the code: To read a file and look for lines which contain the string 'CL'. ranjan das wrote: Hi, I am new to python and i wrote this piece of code which is ofcourse not serving my purpose: Aim of the code: To read a file and look for lines which contain the string 'CL'. Hi, I am new to python and i wrote this piece of code which is ofcourse not serving my purpose: Aim of the code: To read a file and look for lines which contain the string 'CL'. When found, print the entry of the next line (positioned directly below the string 'CL').continue to do this till the end of the file (since there are more than one occurrences of 'CL' in the file) My piece of code (which just prints lines which contain the string 'CL') f=open('somefile.txt','r') for line in f.readlines: if 'CL' in line: print line please suggest how do i print the entry right below the string 'CL'Will 'the next line' ever contains 'CL'?

And if it does, should we printthe next, next line. That is, in case of:abcCLdefghiCLjklmnopqrstshould we print:ghiCLjklmnopqrstor:ghiCLjklif the latter is the case, then you can advance the file's cursor:with open('myfile.txt') as f:for line in f:if 'CL' in line:print next(f)Tutor maillist -To unsubscribe or change subscription options. ranjan das wrote: Hi, I am new to python and i wrote this piece of code which is ofcourse not serving my purpose: Aim of the code: To read a file and look for lines which contain the string 'CL'. When found, print the entry of the next line (positioned directly below the string 'CL').continue to do this till the end of the file (since there are more than one occurrences of 'CL' in the file) My piece of code (which just prints lines which contain the string 'CL') f=open('somefile.txt','r') for line in f.readlines: if 'CL' in line: print line please suggest how do i print the entry right below the string 'CL' Will 'the next line' ever contains 'CL'? And if it does, should we print the next, next line. That is, in case of: abcCLdef ghiCLjkl mnopqrst should we print: ghiCLjkl mnopqrst or: ghiCLjkl if the latter is the case, then you can advance the file's cursor: with open('myfile.txt') as f: for line in f: if 'CL' in line: print next(f) Tutor maillist - To unsubscribe or change subscription options: I like this solution, but it brings a StopIteration error in case thelast line contains a 'CL'Regards,EduardoTutor maillist -To unsubscribe or change subscription options.

File

Tutor How to skip to next line in for loopTutor How to skip to next line in for loop Brain StormerFri May 2 16:05:37 CEST 2008. Previous message:.

Next line python fileNext Line Python File

New Line Python File

Next message:. Messages sorted by:Well,I was somewhat confused with all of the answers so I decided to go withmy/following method. Kent's method has 4 fewer lines of code than mine andcleaner. Please correct me if I am fundamentally wrong.f=open('file.txt',r)for line in f.read.split:if line '3'position = Trueelse:position = Falseif position Trueprint lineposition = Falsef.closeOn Fri, May 2, 2008 at 6:55 AM, Kent Johnson wrote: On Fri, May 2, 2008 at 3:55 AM, Roel Schroeven wrote: Shouldn't it even be 'line = f.next'? Wow, I think Brain Stormer should get a prize. I'm not sure what the prize is, but his short program has elicited incomplete and inaccurate answers from three of the top posters to this list!

I.think. this is a complete answer: f = open('file.txt',r) for line in f: if line.rstrip '3': line = f.next print line break # put this in if there is only one line you want to print f.close.awaiting any further corrections:-) Kent Tutor maillist - - next part -An HTML attachment was scrubbed.URL:. Previous message:.

Next message:. Messages sorted.

Comments are closed.