473,626 Members | 3,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\\Classifie r\Data\\test.tx t"
dataobject = open(datafile," r")

while dataobject.read line() !="":

line = dataobject.read line()
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 8696
On Sun, 19 Oct 2003 20:33:49 -0700, in article
<ma************ *************** **********@pyth on.org>, peter leonard wrote:
while dataobject.read line() !="":

line = dataobject.read line()
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.xrea dlines():
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.read line() !="":
Reads the next line, compares it to the empty string, then throws it
away.
line = dataobject.read line()
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.read line()
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*******@hotm ail.com> wrote:
datafile ="C:\\Classifie r\Data\\test.tx t"
dataobject = open(datafile," r")

while dataobject.read line() !="":

line = dataobject.read line()
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.read line()
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
3478
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 statement when I think it should go into the if statement. I tried swithching to 'n' for the condition but it gave me
2
2276
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 class for awhile and don't know what I'm doing. I have included my (probably wrong) answers for the first few questions. It would be great if someone could tell me what is missing or what I need to work on at least just for the first few. I...
7
2713
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 format file to skip these few bytes of header before doing BULK INSERT? For example, I have a 1 GB data file with 1000 byte header. Except for first 1000 bytes, rest of the data is good for BULK INSERT.
12
1594
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 comments. I'd be grateful if someone could answer them. #include <stdio.h> void main() {
13
2872
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 the "createNav() method"? I thought that because they are in the same class, that the ddlCategory object would be accessible to all methods in this class. Here is the code snippet:
2
1927
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 vector<string>. From that it selects a random word 6 letters or more long. That is the word of the game - the one to make all the anagrams from. After it selects a word, I initialize two more vector<string>'s - unplayed_anagrams and played_anagrams. I want...
49
1615
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
1580
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 list of lines. text.find('@') will return the position of the first occurence of '@', or a negative value if not found.
1
2387
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)); # print "@array\n"; push @points, ;
0
8701
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.