user89
user89

Reputation: 127

Stripping empty lines from the print statement in python2.7

I wanted to print the lines from two text file alternatively. So I wrote the code to read a line from one text file as below

for readfile_number in range(0,file):
        readfile_number = readfile_number + 1
        with open ("navigated_pages.txt","r") as navigated_pages:
            line = navigated_pages.readlines()
            current_line = str((line[readfile_number - 1]).strip())

Then open the other text file, and print the data alternatively but the print statement produces empty lines which I don't require.

In below statement I check whether the created file before the statement has 1 byte if false then print the line as I don't want to print the line when the corresponding file has no data

(In my case even though the file is empty it has 1 byte of data). This the reason for many empty lines which are produced due to not printing when the empty file occurs.

  with open("command_from_page_" + str(readfile_number), "r") as commands_executed_file:
                    empty_file = os.stat("command_from_page_" + str(readfile_number))[6]==1
                    if str(empty_file) == "False":
                        print "   " + current_line.rstrip("\n")
                        for line in commands_executed_file:
                            if line.strip():
                                line = line.replace('), ', ')\n               ')
                                line = line.replace('o, ', 'o\n               ')
                                print "   > Commands : " + "\033[1m\033[32m"
                                print "               "  + line + "\033[0m"

Navigated pages has data something like:

X_0_Gui_Homescreen_EI_Switchview
X_0_Gui_Homescreen_EI_Set
X_0_Gui_Homescreen_EI_Switchview
X_0_Gui_Homescreen_Homescreen
X_0_Gui_Menu_000_Menu_root
X_0_Gui_Menu_100_Menu_Recording
X_0_Gui_Menu_110_Menu_Recording_Project
X_0_Gui_Menu_100_Menu_Recording

Command from file has data something like:

StatusInfoSet (0, 30), StatusInfoSet (0, 26)

The print produces:

   X_0_Gui_Menu_3231_Menu_Outputs_SDI_status
   > Commands : 
               StatusInfoSet (2, 12)
               StatusInfoSet (2, 44)

   X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
   > Commands : 
               CenterMark (2, 2)
               FrameSet (2, 0)
               FrameSet (2, 1)


   X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
   > Commands : 
               FrameSet (2, 0)
               CenterMark (2, 1)
               StatusInfo (2, 1)
               StatusInfo (2, 0)
               SurroundMask (2, 2)











   X_0_Gui_Menu_100_Menu_Recording
   > Commands : 
               MediaCodec (3)
               SetSensorFormat (1)

I would like to how to remove these empty spaces like

   X_0_Gui_Menu_3231_Menu_Outputs_SDI_status
   > Commands : 
               StatusInfoSet (2, 12)
               StatusInfoSet (2, 44)

   X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
   > Commands : 
               CenterMark (2, 2)
               FrameSet (2, 0)
               FrameSet (2, 1)

   X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
   > Commands : 
               FrameSet (2, 0)
               CenterMark (2, 1)
               StatusInfo (2, 1)
               StatusInfo (2, 0)
               SurroundMask (2, 2)

   X_0_Gui_Menu_100_Menu_Recording
   > Commands : 
               MediaCodec (3)
               SetSensorFormat (1)

Upvotes: 1

Views: 113

Answers (1)

ρss
ρss

Reputation: 5315

Here is a quick example. I have two files named as file1.txt and file2.txt. The file1.txt has the following content:

first line in the file1.


Second line in the file1.


Third line in the file1.

I shall copy the contents from the file1.txt to file2.txt and remove the extra lines.

Code:

file1 = open('file1.txt', 'r')
file2 = open('file2.txt', 'w')
for lines in file1.readlines():
    if lines == '\n':
        print 'Empty line'
    else:
        file2.write(lines+'\n')

file1.close()
file2.close()

The content of the file2.txt will be as following:

first line in the file1.

Second line in the file1.

Third line in the file1.

This is just a quick and dirty sample. Hope it helps.

Update: After experimenting a while the following code will produce the syntax you need. The file1.txt contains the following content:

X_0_Gui_Menu_3231_Menu_Outputs_SDI_status
   > Commands : 
               StatusInfoSet (2, 12)
               StatusInfoSet (2, 44)

X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
    > Commands : 
               CenterMark (2, 2)
               FrameSet (2, 0)
               FrameSet (2, 1)


X_0_Gui_Menu_322_Menu_Outputs_SDI_overlays
   > Commands : 
               FrameSet (2, 0)
               CenterMark (2, 1)
               StatusInfo (2, 1)
               StatusInfo (2, 0)
               SurroundMask (2, 2)











X_0_Gui_Menu_100_Menu_Recording
   > Commands : 
               MediaCodec (3)
               SetSensorFormat (1)

then execute this script and see if it matches what you need:

file1 = open('file1.txt', 'r')
file2 = open('file2.txt', 'w')
count = 0
for lines in file1.readlines():
    #print lines
    if (lines[-1:] == '\n') and (len(lines) == 1):
        if count >= 1:
            pass        
        else:
            file2.write(lines)
        count += 1
    else:
        file2.write(lines)
        count = 0

file1.close()
file2.close()

Upvotes: 1

Related Questions