Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading lines that continue in Python

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi there.

I am attempting to read a Fortran file that has the ":" as a continuation character to denote that the function continues on the next line. For example,

{blanks} CALL FUNCTION ( ARG1, ARG2, ARG3
{blanks} : ARG4, ARG5 )

How would I tell Python to know that if it finds a ":" in FUNCTION that it should treat it as the same record?

I've thought of something like:

continue = f_line.find(':')
newf_line = f_line[:continue:]

f_line was already defined earlier in the code.

Any suggestions?
 
First, I'd stay away from defining a variable named, "continue".

Second, You're only looking at lines where the leftmost non-blank character is ":", right? In that case, say your line is read into a string (maybe this is "f_line"?) called "strLine". Then something like:
Code:
if strLine.strip[0]==':':

_________________
Bob Rashkin
 
Bob,

Thanks for your reply. Here are relevant code snippets:

Code:
f_file = open("testf.data",'r')

....

    f_line = f_file.readline()

To answer your question, yes, I'm only looking at lines that have the left most non-blank character as ":"

Does readline fail in this case? Or can I use your suggestion somehow?
 
I think what I said before is still valid. That is, after you read into the variable, f_line, it now holds a string that is all the text read from "testf.data" (including the linefeed). So what I'm suggesting is that after each "...readline", you perform the test that will determine if the first non-blank character is ":". If so you need to concatenate that string with the previous read.

_________________
Bob Rashkin
 
If I good understand, your wish is to create from the original source lines the corrected lines, which are concatenated according to the continuation character ':', which can be in every column but must be the first character in a line. I don't know if it's so in Fortran, but in COBOL, the continuation character must be everytime in column 7.
OK here is the example:
Given is the following source file named fortran.txt:
Code:
CALL FUNCTION1 ( ARG1, ARG2, ARG3
    :                ARG4, ARG5 )
CMD2
CMD3
CALL FUNCTION4 ( ARG1, ARG2, ARG3
:                ARG4, 
                : ARG5 )

Then you can process it as follows:
Code:
f=open("fortran.txt","r")
edited_lines=[]
idx=0
original_lines = f.readlines()
for line in original_lines:
  line = line.strip()
  if line[0]==":":
    # continuation
    edited_lines[idx-1]=edited_lines[idx-1]+' '+ line[1:].lstrip()
  else:
    edited_lines.append(line)
    idx += 1
f.close()

#
print "Result:"
print "-------\n"
# original lines
print "Original Lines:"
j=1
for line in original_lines:
  print "%2d.line: %s" % (j, line.rstrip())
  j +=1
# edited lines
print "\nCorrected lines:"
j=1
for line in edited_lines:
  print "%2d.line: %s" % (j, line)	
  j +=1

The output is
Code:
Result:
-------

Original Lines:
 1.line: CALL FUNCTION1 ( ARG1, ARG2, ARG3
 2.line:     :                ARG4, ARG5 )
 3.line: CMD2
 4.line: CMD3
 5.line: CALL FUNCTION4 ( ARG1, ARG2, ARG3
 6.line: :                ARG4,
 7.line:                 : ARG5 )

Corrected lines:
 1.line: CALL FUNCTION1 ( ARG1, ARG2, ARG3 ARG4, ARG5 )
 2.line: CMD2
 3.line: CMD3
 4.line: CALL FUNCTION4 ( ARG1, ARG2, ARG3 ARG4, ARG5 )

Now you can further parse the corrected lines which are stored in the list edited_lines.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top