[Tutor] Reading only a few specific lines of a file

Jason Conner jasonbconner at gmail.com
Fri May 23 02:41:38 CEST 2008


Hey guys,

Very new to programming Python, and I'm running up against a problem. I am
building a program that will take a text file, search for a string in that
text file. Once it finds the string its looking for, I want to put the next
five lines of text into a variable. Here's what I have so far:

def loadItem(self, objectToLoad):
    x = 0
    wordList = []
    fobj = file()
    fobj.open("itemconfig.txt", 'rU')

    for line in fobj.readline():
        if line == objectToLoad:
            while x != 5
                for word in line.split():
                    wordList.append(word)
                    x += 1

    thing = item(wordList[0], wordList[1], wordList[2], wordList[3],
wordList[4])
    itemList.append(thing)

This, however, highlights my problem.

       "if line == objectToLoad:"

ceases to be true on the second iteration, as it reads in another line. Is
there a better way to do this, or should I just parse the whole document,
and look for the strings I want to find?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080523/3be3d30f/attachment.htm>


More information about the Tutor mailing list