Home » Coding » Python » Putting a line from a text file into a variable, then moving to nextline
Putting a line from a text file into a variable, then moving to nextline [message #15648] Sun, 07 October 2007 08:00 Go to next message
Vernon Wenberg III  is currently offline Vernon Wenberg III
Messages: 1
Registered: October 2007
Junior Member
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?
Re: Putting a line from a text file into a variable, then moving to next line [message #15657 is a reply to message #15648 ] Sun, 07 October 2007 08:09 Go to previous messageGo to next message
Jorge Godoy  is currently offline Jorge Godoy
Messages: 8
Registered: August 2007
Junior Member
Vernon Wenberg III wrote:

> 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:

>>> a = open('a')
>>> help(a.readline)
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(a.readlines)
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
Re: Putting a line from a text file into a variable, then moving [message #15661 is a reply to message #15648 ] Sun, 07 October 2007 08:18 Go to previous messageGo to next message
Tim Chase  is currently offline Tim Chase
Messages: 21
Registered: September 2007
Junior Member
> 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
Re: Putting a line from a text file into a variable, then moving to next line [message #15668 is a reply to message #15648 ] Sun, 07 October 2007 08:45 Go to previous messageGo to next message
Michal Bozon  is currently offline Michal Bozon
Messages: 6
Registered: September 2007
Junior Member
On Sun, 07 Oct 2007 12:00:44 +0000, Vernon Wenberg III wrote:

> 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()
Re: Putting a line from a text file into a variable, [message #15796 is a reply to message #15648 ] Sun, 07 October 2007 13:40 Go to previous message
Tim Williams  is currently offline Tim Williams
Messages: 22
Registered: August 2007
Junior Member
On 07/10/2007, Tim Chase <python.list@tim.thechases.com> wrote:
> > 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()
>>> lines = open('x.txt').read().splitlines()

:)
Previous Topic:How to create a file on users XP desktop
Next Topic:Mysql class works like php
Goto Forum:
  


Current Time: Fri May 16 02:41:00 EDT 2008

Total time taken to generate the page: 0.17306 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software