473,382 Members | 1,204 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,382 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 1506
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.