473,795 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

May 16 '07 #1
7 3916
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_wit h_s(s)
James
May 16 '07 #2
On 2007-05-16, walterbyrd <wa********@ina me.comwrote:
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?
May 16 '07 #3
Grant Edwards wrote:
On 2007-05-16, walterbyrd <wa********@ina me.comwrote:

>>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
May 16 '07 #4
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.tx t', '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
May 17 '07 #5
On May 16, 4:47 pm, walterbyrd <walterb...@ina me.comwrote:
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.

May 17 '07 #6
On May 16, 2:47 pm, walterbyrd <walterb...@ina me.comwrote:
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

May 17 '07 #7
On May 17, 7:47 am, walterbyrd <walterb...@ina me.comwrote:
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

May 17 '07 #8

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

Similar topics

6
4301
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
5
18029
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript function and have JS put this into an array. Then JS would use this array to create a selection list which displays only the names of the drugs. When the user selections one of the drugs, another selection list will be loaded with the avaiable...
3
2178
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant figure out why any help would be much apprechiated 1. Dim line_num As Integer 2. Dim filename2 As String 3.
13
1766
by: Jiho Han | last post by:
Here's the issue. You have a class, Class Person { public int id; public string firstname; public string lastname; }
3
6270
by: Daniel Nogradi | last post by:
Hi list, I have an awk program that parses a text file which I would like to rewrite in python. The text file has multi-line records separated by empty lines and each single-line field has two subfields: node 10 x -1 y 1
2
2152
by: flyzone | last post by:
Goodmorning people :) I have just started to learn this language and i have a logical problem. I need to write a program to parse various file of text. Here two sample: --------------- trial text bla bla bla bla error bla bla bla bla bla bla bla bla on more lines
3
1913
by: walterbyrd | last post by:
How do I test for the end of a file, in such a why that python can tell the EOF from a blank line?
2
1931
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@ @ @ ====="; cout<< "\n\t=====@@@@@@ @ @ @ @ @ @ @ @ @ @@@ =====";
5
2492
by: Davo1977 | last post by:
Analysing text files to obtain statistics on their content You are to write a Perl program that analyses text files to obtain statistics on their content. The program should operate as follows: 1) When run, the program should check if an argument has been provided. If not, the program should prompt for, and accept input of, a filename from the keyboard. 2) The filename, either passed as an argument or input from the keyboard, should...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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
10214
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...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7540
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
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.