Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I tell the difference between the end of a text file, and an empty line in a text file?

Reply
Thread Tools

How do I tell the difference between the end of a text file, and an empty line in a text file?

 
 
walterbyrd
Guest
Posts: n/a
 
      05-16-2007
Python's lack of an EOF character is giving me a hard time.

I've tried:

-----
s = f.readline()
while s:
..
..
s = f.readline()
--------

and

-------
s = f.readline()
while s != ''
..
..
s = f.readline()
-------


In both cases, the loop ends as soon it encounters an empty line in
the file, i.e.


xxxxxxxxxx
xxxxxxxxxxx
xxxxxxx
< - - - loop end here
xxxxxxxxxxxxxx
xxxxxxxxxx
x
< ---- loop should end here

 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      05-16-2007
walterbyrd wrote:
> Python's lack of an EOF character is giving me a hard time.
>
> I've tried:
>

[ stuff ]

for s in f:
do_whatever_with_s(s)


James
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      05-16-2007
On 2007-05-16, walterbyrd <(E-Mail Removed)> wrote:

> Python's lack of an EOF character is giving me a hard time.


No it isn't.

> s = f.readline()
> while s:
> .
> .
> s = f.readline()




> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()



Neither one of your examples is legal Python. Please post real
code.

> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.


No, it doesn't. Not if you've done something reasonable like
this:

f = open('testdata','r')
while True:
s = f.readline()
if not s: break
print repr(s)

or this:

f = open('testdata','r')
s = f.readline()
while s:
print repr(s)
s = f.readline()

Please post real, runnable code. You've done something wrong
and we've no way to guess what it was if you won't show us your
code.

--
Grant Edwards grante Yow! Is something VIOLENT
at going to happen to a
visi.com GARBAGE CAN?
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      05-16-2007
Grant Edwards wrote:
> On 2007-05-16, walterbyrd <(E-Mail Removed)> wrote:
>
>
>>Python's lack of an EOF character is giving me a hard time.

>
>
> No it isn't.
>
>
>>s = f.readline()
>>while s:
>>.
>>.
>>s = f.readline()

>
>
>
>
>>s = f.readline()
>>while s != ''
>>.
>>.
>>s = f.readline()

>
>
>
> Neither one of your examples is legal Python. Please post real
> code.
>
>
>>In both cases, the loop ends as soon it encounters an empty line in
>>the file, i.e.

>
>
> No, it doesn't. Not if you've done something reasonable like
> this:
>
> f = open('testdata','r')
> while True:
> s = f.readline()
> if not s: break
> print repr(s)
>
> or this:
>
> f = open('testdata','r')
> s = f.readline()
> while s:
> print repr(s)
> s = f.readline()
>
> Please post real, runnable code. You've done something wrong
> and we've no way to guess what it was if you won't show us your
> code.
>


I'm guessing it was runnable when he pasted it into google groups.

James
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      05-17-2007
walterbyrd wrote:
> Python's lack of an EOF character is giving me a hard time.
>
> I've tried:
>
> -----
> s = f.readline()
> while s:
> .
> .
> s = f.readline()
> --------
>
> and
>
> -------
> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()
> -------
>
>
> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.


That's just not true. Did you try that code?

>>> open('temp.txt', 'w').write('''\

.... xxxxxxxxxx
.... xxxxxxxxxxx
.... xxxxxxx
....
.... xxxxxxxxxxxxxx
.... xxxxxxxxxx
.... x
.... ''')
>>> while s:

.... print s,
.... s = f.readline()
....
>>> f = open('temp.txt')
>>> s = f.readline()
>>> while s:

.... print s,
.... s = f.readline()
....
xxxxxxxxxx
xxxxxxxxxxx
xxxxxxx

xxxxxxxxxxxxxx
xxxxxxxxxx
x

The file.readline() method returns '\n' for empty lines and '' for
end-of-file.

STeVe
 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      05-17-2007
On May 16, 4:47 pm, walterbyrd <(E-Mail Removed)> wrote:
> Python's lack of an EOF character is giving me a hard time.
>
> I've tried:
>
> -----
> s = f.readline()
> while s:
> .
> .
> s = f.readline()
> --------
>
> and
>
> -------
> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()
> -------
>
> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.
>
> xxxxxxxxxx
> xxxxxxxxxxx
> xxxxxxx
> < - - - loop end here
> xxxxxxxxxxxxxx
> xxxxxxxxxx
> x
> < ---- loop should end here


Use a "for s in f" loop instead.

 
Reply With Quote
 
casevh@gmail.com
Guest
Posts: n/a
 
      05-17-2007
On May 16, 2:47 pm, walterbyrd <(E-Mail Removed)> wrote:
> Python's lack of an EOF character is giving me a hard time.
>
> I've tried:
>
> -----
> s = f.readline()
> while s:
> .
> .
> s = f.readline()
> --------
>
> and
>
> -------
> s = f.readline()
> while s != ''
> .
> .
> s = f.readline()
> -------
>
> In both cases, the loop ends as soon it encounters an empty line in
> the file, i.e.
>
> xxxxxxxxxx
> xxxxxxxxxxx
> xxxxxxx
> < - - - loop end here
> xxxxxxxxxxxxxx
> xxxxxxxxxx
> x
> < ---- loop should end here


Assuming f is initialized as in your example, try

---------
for s in f:
print s
---------

casevh

 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      05-17-2007
On May 17, 7:47 am, walterbyrd <(E-Mail Removed)> wrote:
> Python's lack of an EOF character is giving me a hard time.


The difference is simply that an empty line contains a '\n' while EOF
does not. If you strip() your line before testing you will have
trouble. But the minimal cases you post (properly indented and with
the missing ':' in place), should work (they just won't produce any
output). Repairing the first , I'm using dots (aka stops, periods) for
spaces here to stop the code getting munged :

line = fobj.readline()
while line :
.....print line.strip()
.....line = fobj.realine()

This does work look at this output (and note the empty lines):
line with stuff
line with more stuff

line after the empty line and before another

last line

In python it is more ideomatic to write this general kind of loop with
a break statement, thus:

while True :
.....line = fobj.readline()
.....if not line : break
.....print line.strip()

However since file has for a long time been an iterable the easiest
and most readible way to write it is this:

for line in fobj :
.....print line.strip()

Asun

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
is there a difference between CIR and CIR+end to end clear channel connection? ike lozada Cisco 0 05-27-2004 02:34 AM
Can a BIOS tell the difference between 45ns RDRAM and 40ns RDRAM? Scott Gardner Computer Information 8 05-10-2004 12:17 PM



Advertisments