繁体   English   中英

如何从python中的多行字符串中删除特定的空行?

[英]How to remove specific empty lines from multi line strings in python?

我正在使用模板来创建多个.txt文件。 一些文件将具有空值,因此我想删除产生的空行:

arg1 = '- this is the third line'
arg2 = '- this is the fourth line'
arg3 = ''
arg4 = '- this is the sixth line'

当应用于模板时,结果为以下内容:

(内容为多行字符串)

This is the first line:

    - this is the third line
    - this is the fourth line

    - this is the sixth line

This is some other content whose possible empty lines need to be left alone.

从模板中:

This is the first line:

    $arg1
    $arg2
    $arg3
    $arg4

This is some other content whose possible empty lines need to be left alone.

因此,在将内容写入文件之前,我想删除这些难看的空行,因此看起来像这样:

This is the first line:

        - this is the third line
        - this is the fourth line         
        - this is the sixth line

This is some other content whose possible empty lines need to be left alone.

换句话说,我想删除属于特定行范围的所有空行,如下所示:

for line, index_line in zip(content.splitlines(), range(1, 11)):
    if index_line in range(4, 11) and line == '    ':
        # command that will remove the empty line and save the new content

PS范围是不同的,因为这是我自己的代码段,但是给定示例的范围是:

range (1, 7) #通过第六行时停止

range(3,7) #仅检查给定范围内的行

您想要的功能是list.pop(index)

# assuming you have the contents read from the file split into this list:
lines = content.splitlines()

indicestoremove=[]
for index in range (2,6): # or whatever range of lines you want to trim - 
                          # remember indices start from 0 for the first line
    if lines[index] == '':
        indicestoremove.append(index)

# remove in reverse order, as pop() changes the index of items later in the list
for index in sorted(indicestoremove, reverse=True):
    lines.pop(index)

f = open('filename')
for line in lines:
  f.write("%s\n" % line)

如果范围可能有所不同,并且如果我们可以依靠“ ^-\\ s”作为开始和停止删除空行的标记,则可以使用正则表达式。

import re

s = '''This is the first line:

    - this is the third line
    - this is the fourth line

    - this is the sixth line


This is some other content whose possible empty lines need to be left alone.

Leave that last line alone.
'''

remove_empty = False
lines = []
for line in s.splitlines():
    l = line.strip()
    if l != '':
        dashed = (re.match('^-\s', l) is not None)
        if dashed and not remove_empty:
            # Now we need to start removing empty strings
            remove_empty = (re.match('^-\s', l) is not None)
        elif not dashed and remove_empty:
            # Now it is time to stop
            remove_empty = False
            lines.append('')

    if l != '' or not remove_empty:
        lines.append(line)

print '\n'.join(lines)
# This is the first line:
#
#     - this is the third line
#     - this is the fourth line
#     - this is the sixth line
#
# This is some other content whose possible empty lines need to be left alone.
#
# Leave that last line alone.

如果您确定范围,那么看起来亚伦D会有更好的解决方案。

暂无
暂无

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

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