Python Forum
Print returns empty but no errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print returns empty but no errors
#1
I'mproccesing a text file, searching for lines and printing those to an OutPut file.
Code has 4 'IF' blocks,
If I call "print" after "IF block" 3 it prints my "found lines"
If I call "print" after "IF block" 4 print returns empty lines.
The script actually sees Var from "IF block" 4 and evet prints it to an OutPut file but nothing from the 3 "IF blocks" above.
I'm new to Python Wall and it seems strange to me, no errors and no "print".

Thank you!

Code here:
import os
import re

runtime_l = ',"  Run  Time'
start_tm  = ',"  Start Time'
end_tm    = ',"  End  Time'
program_n = ',"    Test Program Name:'
st_t1 = ''
endt1 = ''
rnt_1 = ''
ftow = open('C:\\01\\sorted_OUT.txt','w')

with open('C:\\02\\en15\\TST2\\Debug_1.log','r') as ftor:
    for ln in ftor:
        #print (ln)
        ln = ln.rstrip()
        if start_tm in ln:
            
            *extraWords,st_t1 = ln.split('Time') # Grtting Start Time line #
            st_t1=st_t1.replace('"','')           
            #print ("Start Time Line -->> " , str_t)
            #print (type(st_t1))
            
        if end_tm in ln:
            #print (ln) 
            *extraWords,endt1 = ln.split('Time') # Grtting END Time line #
            endt1=endt1.replace('"','')
            #print ("ENd Time line -->> " , endt1)            

            
        if runtime_l in ln:
            #print (ln) 
            *extraWords,rnt_1 = ln.split('Time') # Grtting Run Time line #
            rnt_1=rnt_1.replace('"','')
            #ftow.write (str_t+ endt_p+ rnt_p +'\n')  # works here#
            
            
        if program_n in ln :
            #print ("Start Time Line -->> " , str_t1)
            *extraWords,prg_n = ln.split('Name') # Grtting program name line #            
            prg_n=prg_n.replace('"','')
            #print ("Program Name -->> " ,prg_n)
            ftow.write (st_t1 +endt1 + rnt_1 + prg_n + '\n')
            print ("All Lines -->> "+st_t1 +endt1 + rnt_1 + prg_n + '\n')  # does not work 
ftow.close()
Reply
#2
Just found if I move the last "IF" block to the top of the "IF" blocks the script prints all lines just fine.
What does it mean?
with open('C:\\02\\en15\\TST2\\Debug_1.log','r') as ftor:
    for ln in ftor:
        #print (ln)
        ln = ln.rstrip()
        
        if program_n in ln :
            #print ("Start Time Line -->> " , str_t1)
            *extraWords,prg_n = ln.split('Name') # Grtting program name line #            
            prg_n=prg_n.replace('"','')
            #print ("Program Name -->> " ,prg_n)
        
        if start_tm in ln:
            
            *extraWords,st_t1 = ln.split('Time') # Grtting Start Time line #
            st_t1=st_t1.replace('"','')           
            #print ("Start Time Line -->> " , str_t)
            #print (type(st_t1))
            
        if end_tm in ln:
            #print (ln) 
            *extraWords,endt1 = ln.split('Time') # Grtting END Time line #
            endt1=endt1.replace('"','')
            #print ("ENd Time line -->> " , endt1)            

            
        if runtime_l in ln:
            #print (ln) 
            *extraWords,rnt_1 = ln.split('Time') # Grtting Run Time line #
            rnt_1=rnt_1.replace('"','')
            ftow.write (st_t1 +endt1 + rnt_1 + prg_n + '\n')  # works here#

            print ("All Lines -->> "+st_t1 +endt1 + rnt_1 + prg_n + '\n')  # does not work here #

ftow.close()
Reply
#3
I assume you are talking about the print ("All Lines -->>…

Initially 'st+t1, endt1, rnt_1, and prg_n are all ' '. If runtime_l is encountered before start_tm or end_tm or program_n, the print statement will print a bunch of blanks. Moving the "if program_n in ln: does nothing to change the logic of the program. It should act exactly the same way. However you also moved the print from one "if" to another. I can only guess, but I think you want to have the print statement outside the for loop. That way it will wait for all lines to be processed before printing.

By the way, the "ftow.close)_" is not needed. The "with open..." creates a context for the file. When the program exits the context the file automatically closes. It would even close if there was an exception that caused the program to fail.
Reply
#4
Thanks for the reply!
I want to print all my lines I'm looking for into a text file.
Each 'IF' block would find a line and it'll print it to my OutPut file.
The output file will look like this:

Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:
Start Time, End Time, Run Time, Test Program Name:

In Perl, it is as easy as 123 but in Python, this simple task is very complicated and I do not understand how to get around it. "does nothing to change the logic of the program" I would agree with you on this but it seems in Python it does change something. As soon As I move the 'IF' block from the first to last the printing stops.
Reply
#5
Your are having problems with Python because you don't know Python. I couldn't write anything in Perl. That language is crazy hard.

No, moving the if statement up did not change your program. What changed your program is you moved the print statement so it prints if runtime_l in ln. Before it used to print if program_n in line
        if runtime_l in ln:
            #print (ln) 
            *extraWords,rnt_1 = ln.split('Time') # Grtting Run Time line #
            rnt_1=rnt_1.replace('"','')
            ftow.write (st_t1 +endt1 + rnt_1 + prg_n + '\n')  # works here#
 
            print ("All Lines -->> "+st_t1 +endt1 + rnt_1 + prg_n + '\n')  # does not work here #
Code blocks in python are specified by indentation. The print statement above is only executed if runtime_l in n. If you want it to print for each line you need to un-indent the print statement 1 level.
Reply
#6
To deanhystad!
I must agree with you, I do not know Python! And it makes me sad.
I appreciate your help and I see your point. I think my problem is solved.

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inspect.getmembers with isclass returns an empty list Aldar 1 2,790 Oct-02-2019, 01:48 PM
Last Post: Aldar
  Writing chr(128) to text file returns errors. Qwert_Yuiop 5 4,507 Aug-07-2018, 03:52 PM
Last Post: snippsat
  How to remove empty line between two print functions BigEasy 1 2,387 Feb-07-2018, 08:38 AM
Last Post: buran
  Empty variable when using print before readline fstefanov 3 3,675 Oct-23-2017, 02:22 AM
Last Post: fstefanov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020