R
Ruben
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_lines = 0
record = input_file.readline()
while record != "":
try:
record = input_file.readline()
# 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_value= ' '.join(value)
print concatenated_value
if record == "":
empty_string_lines += 1
print " Victor Empty string lines = ", empty_string_lines
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.close()
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_lines = 0
record = input_file.readline()
while record != "":
try:
record = input_file.readline()
# 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_value= ' '.join(value)
print concatenated_value
if record == "":
empty_string_lines += 1
print " Victor Empty string lines = ", empty_string_lines
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.close()