简体   繁体   English

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

[英]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. 然后打开另一个文本文件,并交替打印数据,但是print语句会产生我不需要的空行。

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

(In my case even though the file is empty it has 1 byte of data). (就我而言,即使文件为空,它也有1个字节的数据)。 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)

Here is a quick example. 这是一个简单的例子。 I have two files named as file1.txt and file2.txt . 我有两个名为file1.txtfile2.txt文件。 The file1.txt has the following content: file1.txt具有以下内容:

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. 我将内容从file1.txt复制到file2.txt并删除多余的行。

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: file2.txt的内容如下:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM