Iterators in python are about as easy as “hello world” in most other languages

Iterators in python are so easy. I saw some articles on random sites about building generators to do stuff, but in the end they were only trying to iterate through a file line by line so why make it harder on yourself. Declare an iterator of the type you’re dealing with, like lines in a file, and vrooom, off you go.

f = open(infile,’r')
lines = f.readlines()
i = iter(lines)
while true:
.     try:
.        line = i.next()
.    except StopIteration:
.        pass

Posted by admin   @   28 December 2008

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Google Chrome - Out of beta?
Next Post
New 404 page »
Powered by Wordpress   |   Lunated designed by ZenVerse