473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not able to read blank lines and spaces on a small text file

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.

Ruben

The following is the text file: The first line begins with OrgID.
OrgID: Joe S. Smith
OrgName: Smith Foundation


OrgID: Ronald K.Jones
OrgName: Jones Foundation

The following is my script:

#Open input file to be processed with READ access
input_file = open("C:/Documents and Settings/ruben/My
Documents/Python/text.txt", "r")

empty_string_li nes = 0
record = input_file.read line()
while record != "":

try:

record = input_file.read line()
# Split words separated by delimiter(TAB) or separated by spaces
# into elements of the list "key_value_pair "
key_value_pair = record.split()

key = key_value_pair[0]

# Slice/delete first element of list "key_value_pair "

value = key_value_pair[1:]

# Join all elements from the list "value" and add a "blank space" in
# between elements

concatenated_va lue= ' '.join(value)

print concatenated_va lue

if record == "":

empty_string_li nes += 1
print " Victor Empty string lines = ", empty_string_li nes
break
# Get values from table

if key == "OrgID:":
org_id = value

elif key == "OrgName:":
org_name = value

elif record == ' ':
print "Blank line"

elif record == '':
print "END OF FILE"

print "RECORD = ", record

except IndexError:

break
if record == "":
print "EOF", record

elif record == '\0':

print "NULL Characters found"
elif record == "\n":

print "Newline found"

elif record == " ":

print "Blank line found"

# Close file

input_file.clos e()
Jul 18 '05 #1
6 4295
Ruben wrote:
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.


My brain is not really working but a blank line is not how python processes a
the EOF.

Try something like:

input_file = open("C:/Documents and Settings/ruben/My
Documents/Python/text.txt", "r")

for line in input_file:
print line,
Jul 18 '05 #2
On 13 Sep 2004 07:48:14 -0700 Ruben wrote:
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.
An empty record is not the same as "no record"...

Change this:
while record != "":


to:

while record:
Jul 18 '05 #3
Ruben wrote:
while record != "":
try:
record = input_file.read line()
key_value_pair = record.split()
key = key_value_pair[0]
value = key_value_pair[1:]
concatenated_va lue= ' '.join(value)
if record == "":
empty_string_li nes += 1
print " Victor Empty string lines = ", empty_string_li nes
break
except IndexError:
break


The first time this code reads a line which doesn't
contain whitespace separated records, the record.split()
call will return an empty list, and the next line will
try to retrieve the first element from it, raising an
IndexError, which will terminate the loop.

Your code doesn't seem to be following any of the usual
Python idioms. I suggest starting with the following pattern
instead, and growing the code from there:

input_file = open(...)
try:
for record in input_file:
if record.strip() == '': # blank line, ignore
continue

# record-processing code follows here
key_value_pair = record.split()
key = key_value_pair[0]
... etc....

finally:
input_file.clos e()
The above pattern will allow the record-processing code
to handle *only* non-blank lines (including lines that
have just empty whitespace), simplify everything immensely.

-Peter
Jul 18 '05 #4
Peter Hickman wrote:
Ruben wrote:
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.

My brain is not really working but a blank line is not how python
processes a the EOF.


Actually, it is when using things like .readline(), which return even
the newline \n at the end of the line...

-Peter
Jul 18 '05 #5
I think you were trying to make this a little harder
than it actually is. Try this:

#Open input file to be processed with READ access
input_file = open("C:/pytest.txt", "r")
empty_string_li nes=0
#
# Use the fact that you can iterate over a file
# and you don't have to call readline or worry
# with EOF issues. input_file will return a single
# record each time through the loop and fall out
# at EOF.
#
for record in input_file:
record=record.s trip() # Strip trailing "\n"
if not record:
empty_string_li nes+=1
print "Blank line"
continue

#
# Split words separated by delimiter(TAB) into elements key,value"
# Limit split to first tab and the value is left intact.
#
try: key, value=record.sp lit('\t',1)
except:
print "Bad record skipped, record=", record
print "records must be of format key:<tab>value"
continue

print 'key=', key,' value=',value
#
# Get values from table
#
if key == "OrgID:": org_id = value
elif key == "OrgName:": org_name = value
#
# Do something else with the values here
#

# Close file
print "END OF FILE, empty_string_li nes=", empty_string_li nes
input_file.clos e()

Larry Bates
Syscon, Inc.
"Ruben" <fe************ ***@hotmail.com > wrote in message
news:2e******** *************** *@posting.googl e.com...
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.

Ruben

The following is the text file: The first line begins with OrgID.
OrgID: Joe S. Smith
OrgName: Smith Foundation


OrgID: Ronald K.Jones
OrgName: Jones Foundation

The following is my script:

#Open input file to be processed with READ access
input_file = open("C:/Documents and Settings/ruben/My
Documents/Python/text.txt", "r")

empty_string_li nes = 0
record = input_file.read line()
while record != "":

try:

record = input_file.read line()
# Split words separated by delimiter(TAB) or separated by spaces
# into elements of the list "key_value_pair "
key_value_pair = record.split()

key = key_value_pair[0]

# Slice/delete first element of list "key_value_pair "

value = key_value_pair[1:]

# Join all elements from the list "value" and add a "blank space" in
# between elements

concatenated_va lue= ' '.join(value)

print concatenated_va lue

if record == "":

empty_string_li nes += 1
print " Victor Empty string lines = ", empty_string_li nes
break
# Get values from table

if key == "OrgID:":
org_id = value

elif key == "OrgName:":
org_name = value

elif record == ' ':
print "Blank line"

elif record == '':
print "END OF FILE"

print "RECORD = ", record

except IndexError:

break
if record == "":
print "EOF", record

elif record == '\0':

print "NULL Characters found"
elif record == "\n":

print "Newline found"

elif record == " ":

print "Blank line found"

# Close file

input_file.clos e()

Jul 18 '05 #6
Ruben

I hope you don't mind what I'm going to say. Your current solution is
a bit confusing, and there are better idioms in Python to solve your
problem. It seems that you either tried to port a program written in
other language such as C, or written this one with a C-like mind.
There are several reasons why we here like Python, but writing C-like
code is not one of them. Said that, I have to warn you that I'm not a
Python expert (there are a few terrific ones around here) and that my
opinion here is given as an non-authoritative advice. Be warned :-)

To loop over the lines in a text file, use the following snippet:

input_file = open("C:\\work\ \readlines.txt" , "r")
for line in input_file.read lines():
print "[",line,"]"

There is no need to do a loop like you did. The loop above will check
all conditions - EOF< empty files, and so on. Now, in order to process
your lines, you need to write something like a state machine. It's
easier done than said. You just have to read line by line, checking
what you have read, and building the complete record as you go. Try
this -- it's heavily commented, but it's very short:

input_file = open("C:\\work\ \readlines.txt" , "r")

import string

for line in input_file.read lines():
# line may still have the /n line ending marker -- trim it
# it will also remove any extraneous blank space. it's
# not actually mandatory, but helps a little bit if you
# need to print the line and analyze it.
line = line.strip()

# we'll use the split function here because it's simpler
# you can also use regular expressions here, but it's
# slightly more difficult to read first time. Let's keep
# it simple. maxsplit is a keyword parameter that tells
# split to stop after doing finding the first splitting
# position.
try:
field_name, field_value = string.split(li ne, maxsplit=1)
except:
# if it can't properly split the line in two, it's
# either an invalid record or a blank line. Just
# skip it and continue
continue

if field_name == "OrgID:":
record_id = field_value
if field_name == "OrgName:":
record_value = field_value
# assuming that this is the last value read,
# print the whole record
print record_id, "-", record_value

input_file.clos e()

The result is:

Joe S. Smith - Smith Foundation
Ronald K.Jones - Jones Foundation

Please note that I purposefully avoided defining new classes here or
using other common Python constructs. The solution could be much more
elegantly written than this, but I still hope to have helped you.

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmai l.com
mail: ca********@yaho o.com
Jul 18 '05 #7

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

Similar topics

12
3286
by: dan glenn | last post by:
Hi. I'm finding that if I have text entered into a <textarea ...> </textarea> in a form, and that text has leading blank lines (with no spaces or anything else), that when I retrieve the entered text, I lose the leading blank line. Each time it goes through a post any top-leading blank line is lost. Only one- if I have several blank lines in a row at the top, only the first is lost. (If the top blank line has a single space, it's handled...
58
4689
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split (PHONE_LIST, "_+")) { Console.WriteLine (phoneNumber); } } Output: 495.1000
4
2064
by: DraguVaso | last post by:
Hi, I have files I need to read, which contains records with a variable lenght. What I need to do is Copy a Part of such a File to a new File, based on the a Begin- and End-record. I used this functions: Dim intMyFile As Integer = FreeFile() FileOpen(intMyFile, MakePathFile(strDirS, strFileS), OpenMode.Input, OpenAccess.Read, OpenShare.Shared, -1)
3
3703
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank lines in the output. Here is a simplified XML and XSLT: (Note the problem does not happen when testing in XMLSpy) - - - - - - - - - - - - - - - - - - - - - - - -
3
1810
by: Dan Aldean | last post by:
Hello, I have a small project and it's hard to understand what the requirements are. Basically I need to read a text file and identify titles and attributes with their values. I have to remove leading and trailing spaces in the titles and attribute lines. That is OK as I found a way of using only stringbuilder objects. But I find it difficult to understand the following:
2
4749
by: spifster | last post by:
Hello all, I am building a collapsable tree using Javascript with DOM in IE. In order to make collapsed cells disappear I have been hiding the text. The cells collapse but still leave borders behind. I have set the borderStyle to none and the black lines go away, but there is still white space where the borders were. Following are my html files I am using to test it. ----------------BEGIN HTML FILE----------------
9
5209
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
4
2595
by: kaushikshome | last post by:
Can anyone please help me in writing the code in C++ : I want a read a file line by line,remove the lines which have length of a particular field in a line exceeding 63 characters After removing the line the corresponding blank spaces generated after removal of the line also needs to be removed. .. I am in urgent need for this solution.Kindly help how to go about it. Thanks in advance ,
7
3129
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
0
8788
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
9476
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...
1
9263
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
9208
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...
0
6053
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
4570
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
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
3
2193
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.