简体   繁体   中英

Python: How do you read a chunk of text from a file without knowing how long the file actually is?

What I'd like to do is basically I have this file with data in separate lines, except the last piece which is a biography and may stretch across many lines. The biography may be any number of lines long, and all I know is that it starts on the 5th line. Now what I need is a way to retrieve the biography from the fifth line to the end of the file, but I don't know how to do this. Thanks in advance.

Here's what I tried:

from tkinter import *
import os

class App:

    charprefix = "character_"
    charsuffix = ".iacharacter"
    chardir = "data/characters/"


    def __init__(self, master):
        self.master = master
        frame = Frame(master)
        frame.pack()

        # character box
        Label(frame, text = "Characters Editor").grid(row = 0, column = 0, rowspan = 1, columnspan = 2)
        self.charbox = Listbox(frame)
        for chars in []:
            self.charbox.insert(END, chars)
        self.charbox.grid(row = 1, column = 0, rowspan = 5)
        charadd = Button(frame, text = "   Add   ", command = self.addchar).grid(row = 1, column = 1)
        charremove = Button(frame, text = "Remove", command = self.removechar).grid(row = 2, column = 1)
        charedit = Button(frame, text = "    Edit    ", command = self.editchar).grid(row = 3, column = 1)

        for index in self.charbox.curselection():
            charfilelocale = self.charbox.get(int(index))
            charfile = open(app.chardir + app.charprefix + app.charfilelocale, 'r+')
            charinfo = str.splitlines(0)

If you just want to put the entire biography in a string, you can do this:

with open('biography.txt') as f:
    for i in range(4): # Read the first four lines
        f.readline()
    s = ''
    for line in f:
        s += line

" for line in f " iterates over f . iter(f) returns a generator function that yields f.readline() until the end of the file is reached.

Another way to phrase your question would be "how do I discard the first four lines of a file I read?" Taking the answer to that a step at a time:

filename = "/a/text/file"
input_file = open(filename)

where the default mode for open() is 'r' so you don't have to specify it.

contents = input_file.readlines()
input_file.close()

where readlines() returns a list of all the lines contained in the input file in one gulp. You were going to have to read it all anyway, so let's do it with one method call. And, of course close() because you are a tidy coder. Now you can use list slicing to get the part that you want:

biography = contents[4:]

which didn't actually throw away the first four lines, it just assigned all but the first four to biography. To make this a little more idiomatic gives:

with open(filename) as input_file:
    biography = input_file.readlines()[4:]

The with context manager is useful to know but look it up when you are ready. Here it saved you the close() but it is a little more powerful than just that.

added in response to comment :

Something like

with open(filename) as input_file:
    contents = input_file.readlines()
person = contents[0]
birth_year = contents[1]
...
biography = contents[4:]

but I think you figured that bit out while I was typing it.

f = open('workfile', 'w')

for line in f: print line,

This is the first line of the file.
Second line of the file

Python does not require you to know in advance how big a file is or how many lines it contains. It uses an iterator and gets the lines from the file and returns lines lazily. find some excellent documentation here: http://docs.python.org/2/tutorial/inputoutput.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM