Python read next line in file




















Martjin's answer is better granted but this gives you next line at least now it does.. The simplest way to do that is to replace for line in file. BostonJohn BostonJohn 2, 2 2 gold badges 25 25 silver badges 48 48 bronze badges. Martin posted his answer while I was fighting with the editor.

His is a little more svelte, but it's still worthwhile to be aware of the enumerate option. Coming across this answer in it bears pointing out that 'xrange' does not exist in Python 3 and 'range' should be used instead. Doesn't this approach read the entire file into memory, in contrast to the method that uses the file as an iterator for line in infile:?

That may be a trivial difference to some, but could matter quite a lot if you are dealing with very large files. The iterator method is inherently more efficient. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. To complicate matters, Python offers several solutions for reading files. These functions simplify file management by handling some of the work for us, behind the scenes.

Our first approach to reading a file in Python will be the path of least resistance: the readlines method. This method will open a file and split its contents into separate lines. This method also returns a list of all the lines in the file. We can use readlines to quickly read an entire file. We need to read this file and do something with the data.

Unlike its counterpart, the readline method only returns a single line from a file. The realine method will also add a trailing newline character to the end of the string. With the readline method, we also have the option of specifying a length for the returned line. If a size is not provided, the entire line will be read. Active Oldest Votes.

So, the process is very simple: Find the current position within the file using tell. Perform a read or write operation of some kind. Improve this answer. Henry Keiter Henry Keiter 16k 7 7 gold badges 46 46 silver badges 77 77 bronze badges.

It doesn't work for me. It return OSError: telling position disabled by next call — robertspierre. Nice approach; definitely meets the OP's "Pythonic" request. My only concern would be what to do if a larger perhaps arbitrary number of "reads" is required? Just increasing the number of iterators seems like it would get messy very quickly.

A little ugly but maybe useful depending on the application. HenryKeiter, if it is known that no more than x "seen" lines would be needed at arbitrary times, one option involves populating an x -sized deque with each line as it is read and using it as a buffer — iruvar.

I'm more concerned with the case where it isn't known how many times a certain chunk of the file might need to be read. Of course it may be better to use a single f. Here's a simple way to do it for a file object: with open 'foo. I had a similar problem and using the continue statement instead worked in my case.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. Related



0コメント

  • 1000 / 1000