Reading n lines from file (but not all) in Python -


how read n lines file instead of 1 when iterating on it? have file has defined structure , this:

for line1, line2, line3 in file:     do_something(line1)     do_something_different(line2)     do_something_else(line3) 

but doesn't work:

valueerror: many values unpack

for doing this:

for line in file:     do_someting(line)     newline = file.readline()     do_something_else(newline)     newline = file.readline()     do_something_different(newline) ... etc. 

which sucks because writing endless 'newline = file.readline()' cluttering code. there smart way ? (i want avoid reading whole file @ once because huge)

basically, fileis iterator yields file 1 line @ time. turns problem how yield several items @ time iterator. solution given in this question. note function isliceis in itertools module have import there.


Comments

Popular posts from this blog

400 Bad Request on Apache/PHP AddHandler wrapper -

Add email recipient to all new Trac tickets -

php - Change action and image src url's with jQuery -