1

Im trying to take a text file and use only the first 30 lines of it in python. this is what I wrote:

text = open("myText.txt")
lines = myText.readlines(30)
print lines

for some reason I get more then 150 lines when I print? What am I doing wrong?

1
  • 6
    Shouldn't be lines = text.readlines(30)? Commented Nov 30, 2011 at 18:48

4 Answers 4

5

Use itertools.islice

import itertools

for line in itertools.islice(open("myText.txt"), 0, 30)):
    print line
Sign up to request clarification or add additional context in comments.

3 Comments

This solution seems to be affected by the same limitation of @ShawnChin one: it appears the entire file is loaded into memory before the slicing. I got: [1.9277660846710205, 1.9260480403900146, 1.9186549186706543] for a file of about 500 lines, and [1.5532219409942627, 1.5311739444732666, 1.5274620056152344] for one of 50, but I would appreciate cross-checking my findings...
@mac no it doesn't. If you pass a file object into islice and repeat the operation twice, you'll see that it continues where it left off i.e. the file was not read till the end.
@ShawnChin - Thank you for this, it's most definitively a better way to test than using times as I did! :)
4

If you are going to process your lines individually, an alternative could be to use a loop:

file = open('myText.txt')
for i in range(30):
    line = file.readline()
    # do stuff with line here

EDIT: some of the comments below express concern about this method assuming there are at least 30 lines in the file. If that is an issue for your application, you can check the value of line before processing. readline() will return an empty string '' once EOF has been reached:

for i in range(30):
    line = file.readline()
    if line == '':  # note that an empty line will return '\n', not ''!
        break
    index = new_index
    # do stuff with line here

7 Comments

@CésarBustíos - right, I just tried it locally with a smaller file and did not remember to update the code. Fixed!
This only works if you know there are 30 lines. Otherwise the last readlines() will return "".
@AndrewDalke - did you mean "if you know there are at least 30 lines" and then readline() instead of readlines()?
@mac he meant if the file has less than 30 lines, then the remaining calls to readline() will return "". You're still iterating through 30 values even if there are less lines in the file.
@mac: yes, and yes. "If there are 5 people waiting for a cashier then open up a new register" doesn't mean that if 6 people are waiting then a new register won't open up. And by "readlines()" I meant "readline()s", meaning "final calls to readline()".
|
2

The sizehint argument for readlines isn't what you think it is (bytes, not lines).

If you really want to use readlines, try text.readlines()[:30] instead.

Do note that this is inefficient for large files as it first creates a list containing the whole file before returning a slice of it.

A straight-forward solution would be to use readline within a loop (as shown in mac's answer).

To handle files of various sizes (more or less than 30), Andrew's answer provides a robust solution using itertools.islice(). To achieve similar results without itertools, consider:

output = [line for _, line in zip(range(30), open("yourfile.txt", "r"))]

or as a generator expression (Python >2.4):

output = (line for _, line in zip(range(30), open("yourfile.txt", "r")))
for line in output:
    # do something with line.

1 Comment

Not entirely sure, but won't this read all the lines into memory for then keeping just the first 30?
0

The argument for readlines is the size (in bytes) that you want to read in. Apparently 150+ lines is 30 bytes worth of data.

Doing it with a for loop instead will give you proper results. Unfortunately, there doesn't seem to be a better built-in function for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.