Putting a line from a text file into a variable, then moving to nextline

  • Thread starter Vernon Wenberg III
  • Start date
V

Vernon Wenberg III

I'm not really sure how readline() works. Is there a way to iterate
through a file with multiple lines and then putting each line in a
variable in a loop?
 
J

Jorge Godoy

Vernon said:
I'm not really sure how readline() works. Is there a way to iterate
through a file with multiple lines and then putting each line in a
variable in a loop?

To know how something works you can always check the docs about this
specific functionality:
Help on built-in function readline:

readline(...)
readline([size]) -> next line from the file, as a string.

Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
Help on built-in function readlines:

readlines(...)
readlines([size]) -> list of strings, each a line from the file.

Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.


If you are creating new variables on every loop iteration you might be
interested in two things:

- you can loop directly through the file, on a line by line basis
- you can assign the read line to a an array
 
T

Tim Chase

I'm not really sure how readline() works. Is there a way to iterate
through a file with multiple lines and then putting each line in a
variable in a loop?

You can use readlines() to get the whole line (including the
newline):

lines = file('x.txt').readlines()

or you can iterate over the file building a list without the newline:

lines = [line.rstrip('\n') for line in file('x.txt')]

Thus, line[0] will be the first line in your file, line[1] will
be the second, etc.

-tkc
 
M

Michal Bozon

I'm not really sure how readline() works. Is there a way to iterate
through a file with multiple lines and then putting each line in a
variable in a loop?

There are always more ways how to do it.. one of them is:

f = open(filename)
for line in f:
# you may then strip the newline:
line = line.strip('\n')
# do anything you want with the line
f.close()
 
T

Tim Williams

I'm not really sure how readline() works. Is there a way to iterate
through a file with multiple lines and then putting each line in a
variable in a loop?

You can use readlines() to get the whole line (including the
newline):

lines = file('x.txt').readlines()

or you can iterate over the file building a list without the newline:

lines = [line.rstrip('\n') for line in file('x.txt')]

Thus, line[0] will be the first line in your file, line[1] will
be the second, etc.

or splitlines()
:)
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top