How do you limit the # of lines Read?

K

koutoo

I am working on a loop for my code and was wondering if there is a way
to limit the number of lines read through? I'd hate to cut the test
file in order to run the code in the testing phase. Can you add a
value in the parenthesis of the readline() function?

t = string.readline() # Limit this somehow?


Kou
 
M

Marc 'BlackJack' Rintsch

I am working on a loop for my code and was wondering if there is a way
to limit the number of lines read through? I'd hate to cut the test
file in order to run the code in the testing phase. Can you add a
value in the parenthesis of the readline() function?

t = string.readline() # Limit this somehow?

Do you want to limit how much of *one* line is read or the number of lines!?

The latter can be done with `itertools.islice()`.

Ciao,
Marc 'BlackJack' Rintsch
 
P

Paul McNett

I am working on a loop for my code and was wondering if there is a way
to limit the number of lines read through? I'd hate to cut the test
file in order to run the code in the testing phase. Can you add a
value in the parenthesis of the readline() function?

t = string.readline() # Limit this somehow?

Assuming s is a file-like object:

for idx, line in enumerate(s):
if idx > 2:
break
print idx, line
 
J

John Machin

I am working on a loop for my code and was wondering if there is a way
to limit the number of lines read through? I'd hate to cut the test
file in order to run the code in the testing phase. Can you add a
value in the parenthesis of the readline() function?

t = string.readline() # Limit this somehow?

*STRING*.readline() ????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top