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

2008-05-22 Thread Jason Conner
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?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2008-05-24 Thread Jason Conner
Hey guys,

I went with a combination of the above, though I tried several different
solutions. Here's what I ended up with:

def loadItem(self, objectToLoad):
wordList = []
fobj = open('/home/jason.conner/Documents/Python/objectconfig.txt', 'r')

for line in fobj:
if line == objectToLoad:
wordList.append(line)
for i in range(5):
wordList.append(fobj.next())

Though it did take me a few minutes to figure out that I needed to include
the \n in the objectToLoad variable to get it to work in its present form,
it works great. Thanks for all your help!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Python from books

2008-06-19 Thread Jason Conner
I use the same books - Learning Python and Core Python Programming, 2nd ed.
I found I got about halfway through Learning Python before I switched to CPP
and had no problems. I also use "Python Phrasebook" (Brad Dayley, 2007) as a
handy reference guide to some common problems as well.

Core Python Programming, 2nd ed. (Wesley Chun, 2007) is my most frequent
instructional guide, though.

On Fri, Jun 20, 2008 at 9:39 AM, jay <[EMAIL PROTECTED]> wrote:

> Me personally, both "Learning Python" and "Core Python Programming".  I am
> by no means an expert, but both of these books are excellent and were quite
> helpful.
>
> jay
>
>
> On Thu, Jun 19, 2008 at 1:56 PM, Zameer Manji <[EMAIL PROTECTED]> wrote:
>
>> -BEGIN PGP SIGNED MESSAGE-
>> Hash: SHA512
>>
>> Has anyone here attempted to learn Python from books ? I recently
>> purchased "Learning Python" 3rd Edition (9780596513986) and if anyone
>> here is a good bottom-up learner than it is the perfect book. The author
>> goes over each feature in python, explaining it's syntax, usage and the
>> "pythonic" way of using them in programs. It has really helped me
>> understand some of the more powerful tools in python and I am sure it
>> will make an excellent reference book in the future. With that said, has
>> anyone else attempted to learn python from books and if so which one ?
>> -BEGIN PGP SIGNATURE-
>> Version: GnuPG v1.4.7 (MingW32)
>>
>> iQEcBAEBCgAGBQJIWqvxAAoJEA759sZQuQ1BS+kIAK+6TWqesUQjEQygwsMI3seU
>> ZWUkd4wlextCOXIH6mse4TMHdAU3+NHDo1V4QwSYxNTEjwpWFr1Kg2BE0zyMyxPD
>> gs8Kaov6/DGQgyFFt5DRcKvkQ0M5St7I/PuP+n/eelMUK1culvXx3oycKBqh8D21
>> R/g0SQ0M9pUDLuBEygbgjFw1WVsf/h8/zCc38qHQ+QOQvrVaEciV3I5JLyp3+u8g
>> JyjzxjHSRHd4Ik4cnaeKa2OyFn5JaPXxPmrmPiE3WEWnJxLWucXAP/CEm7e9aamQ
>> Fv4SYuVmOIGpbAqySgEpQH2yRsd+0qaNKSJq4hqjQ1/z2Bx6hM5LTqJAVjfZMW4=
>> =9xVw
>> -END PGP SIGNATURE-
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor