473,231 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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

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?
Oct 7 '07 #1
4 1500
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
Oct 7 '07 #2
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


Oct 7 '07 #3
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()
Oct 7 '07 #4
On 07/10/2007, Tim Chase <py*********@tim.thechases.comwrote:
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()
:)
Oct 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
2
by: Michael Powe | last post by:
Hello, I have written a small app to parse web log files and extract certain lines to another file. There is also functionality to count all the items that are being filtered out. I wrote...
6
by: Adrian Lin | last post by:
Is there a way to read a specfic line in a text file? For instance if I wanted to just read the 5th line is that possible without having to step through the other lines? Essentially I am doing huge...
9
by: Shivam | last post by:
Hi All, I have a mysql dump file... something like. __________________________________________________ CREATE TABLE nation ( ip int(11) unsigned NOT NULL default '0', country char(2) NOT...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
6
by: Jacob Rael | last post by:
Hello, I have a simple script to parse a text file (a visual basic program) and convert key parts to tcl. Since I am only working on specific sections and I need it quick, I decided not to...
15
by: eight02645999 | last post by:
hi, how can i skip printing the last line using loops (for /while) eg for line in open("file): print line. I want to skip printing last line of the file.thanks
4
by: analoveu | last post by:
how can I print new line in a file using a reference to Formatter object //create reference to file to read from Scanner input=new Scanner(new File("d:\\records1.txt")); //create reference to...
2
by: AZRebelCowgirl73 | last post by:
Crazy as this sounds this program is actually working! I know I know it is amazing I got one to run. The only problem I am having is I want it to say for output line (1,2 3 etc.) has a length...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.