473,396 Members | 1,864 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,396 software developers and data experts.

Beginner question : skips every second line in file when usingreadline()

Hi,
I having a problem with reading each line from a text file. For example, the
file is a text file named 'test.txt' with the following content :

line 1
line 2
line 3
line 4
line 5

The following script attempts to print out each line :

datafile ="C:\\Classifier\Data\\test.txt"
dataobject = open(datafile,"r")

while dataobject.readline() !="":

line = dataobject.readline()
print line

However, the output from this script is :

line 2

line 4
I'm sure this is a simple problem but I can't figure it after loking up
several reference books and web pages. Any help would be greatly
appreciated.

Regards
Peter

__________________________________________________ _______________
Want to check if your PC is virus-infected? Get a FREE computer virus scan
online from McAfee.
http://clinic.mcafee.com/clinic/ibuy...n.asp?cid=3963
Jul 18 '05 #1
3 8677
On Sun, 19 Oct 2003 20:33:49 -0700, in article
<ma*************************************@python.or g>, peter leonard wrote:
while dataobject.readline() !="":

line = dataobject.readline()
print line

However, the output from this script is :

line 2

line 4


You're reading a line in the "while:" statement by calling readline(), but
the line being read isn't used. Then, you call readline() again in the
body of the loop. That's the only input you're going to see in your
output, the even-numbered lines.

Do something like this instead:

for line in dataobject.xreadlines():
print line

I'm no Python expert, so there may be a better way. The code above works
for me.
Jul 18 '05 #2
On Sun, 19 Oct 2003 20:33:49 -0700, peter leonard wrote:
while dataobject.readline() !="":
Reads the next line, compares it to the empty string, then throws it
away.
line = dataobject.readline()
Reads the next line and assigns it to the 'line' variable.
print line


Prints out the 'line' variable.

I think you can see where the problem is.

Possibly you want something like this:

while( True ):
line = dataobject.readline()
if( line == "" ):
break
print line

--
\ "Most people don't realize that large pieces of coral, which |
`\ have been painted brown and attached to the skull by common |
_o__) wood screws, can make a child look like a deer." -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
"peter leonard" <pf*******@hotmail.com> wrote:
datafile ="C:\\Classifier\Data\\test.txt"
dataobject = open(datafile,"r")

while dataobject.readline() !="":

line = dataobject.readline()
print line


The basic problem is that you're calling readline() twice each time
around the loop. Once in the test part of the while (where you test and
then throw away the returned value), and again in the body. Of course
you're only getting every other line! You want to do something like
this:

while 1:
line = dataobject.readline()
if line == "":
break
print line
Jul 18 '05 #4

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

Similar topics

7
by: Sonoman | last post by:
I am trying to do this: cin >> temp; if (temp == "n"){ Then do something... } temp was declared as a string and the input I give at the prompt is n, but it skips the condition for the if...
2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
7
by: Bob | last post by:
Hi, I am trying to use BULK INSERT with format file. All of our data has few bytes of header in the data file which I would like to skip before doing BULK INSERT. Is it possible to write...
12
by: Sathyaish | last post by:
Please forgive my nescience. I have worked extensively on Win32 but I am only familiar with C and C++. Lately, I have been practicing C from K&R. Here 're a few doubts I have written in the...
13
by: Ranginald | last post by:
I am new to oop so please bear with me: Could someone please explain to me why the "protected void runCategory() method" cannot access the local ddlCategory object that is easily accessed in...
2
by: theronnightstar | last post by:
I am writing an anagram program for my fiance. Figured it would be an excellent task to learn from. The way it is supposed to work is it reads in a word list from a file into a temporary...
49
by: W. Watson | last post by:
Is there an editor that allows one to position to put the cursor and then by pushing some button goes to the end of the def? -- Wayne Watson (Nevada City, CA) Web Page: <speckledwithStars.net>
1
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit : If the file you're reading is not too big, you can use file.readlines() which read all the files and returns its content as a...
1
by: Arjun234 | last post by:
hi, I have a program to calculate the distance. its like this: open(IN, "/path/outModified.pl") or die "$!"; while (my $line = <IN>) { chomp($line); my @array = (split (/\s+/, $line)); #...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.