从python2.7中的print语句中删除空行

时间:2014-11-28 11:13:10

标签: python

我想要从两个文本文件中打印行。所以我编写了代码来读取一个文本文件中的一行,如下所示

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())

然后打开另一个文本文件,并交替打印数据,但print语句产生我不需要的空行。

在下面的语句中,我检查语句之前创建的文件是否有1个字节(如果为false)然后打印该行,因为我不想在相应文件没有数据时打印该行

(在我的情况下,即使文件为空,它也有1个字节的数据)。这是由于在空文件发生时不打印而产生许多空行的原因。

  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"

导航页面的数据类似于:

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

来自文件的命令具有如下数据:

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

印刷品产生:

   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)

我想如何删除这些空格,如

   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)

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子。我有两个名为file1.txtfile2.txt的文件。 file1.txt具有以下内容:

first line in the file1.


Second line in the file1.


Third line in the file1.

我会将内容从file1.txt复制到file2.txt并删除额外的行。

代码

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()

file2.txt的内容如下:

first line in the file1.

Second line in the file1.

Third line in the file1.

这只是一个快速而肮脏的样本。希望它有所帮助。

更新:经过一段时间的实验,以下代码将生成您需要的语法。 file1.txt包含以下内容:

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)

然后执行此脚本并查看它是否符合您的需要:

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()