Python:如果用反斜杠分隔,则连接连续行并删除反斜杠

时间:2015-06-23 10:01:58

标签: python

我有一个包含shell命令的文件。每个命令可以分成多行(在末尾使用反斜杠):
例如

  cmd1 -opt1 \  
    -opt2 val2  \  
    -opt3 val3 val4

如果在结尾处用反斜杠分隔,我想连接连续的行。我也希望在加入后删除反斜杠。

问题类似于:
输入:
['abc','def \\','ghi \\','jkl','yyy \\','zzz']

输出:
['abc','def ghi jkl','yyy zzz']

循环遍历列表唯一的解决方案?

with open("cmd", "r") as fp:
    cmd = ""
    cont = False
    list = []
    for line in fp:
        line = line.rstrip("\n")
        if line.endswith('\\'):
            line = line[:-1]
            if cont:
                cmd = cmd + line
                continue
            if cmd:
                list.append(cmd)
            cmd = line
            cont = True
        else:
            if cont:
                cmd = cmd + line
                if cmd:
                    list.append(cmd)
                cmd = ""
                continue
            cont = False
    print(list)

2 个答案:

答案 0 :(得分:3)

cmd = ['abc','def \\','ghi \\','jkl','yyy \\','zzz']

res =“\ n”.join(cmd).replace(“\\\ n”,“”)。splitlines()

打印(RES)

=> ['abc','def ghi jkl','yyy zzz']

CLODION

答案 1 :(得分:1)

  

循环遍历列表唯一的解决方案?

您必须评估每一行的连接指示符并附加下一行。是的,你必须循环。

这是使用生成器来完成它的功能,因此整个输入不需要在内存中。

>>> sample = ['abc', 'def \\', 'ghi \\', 'jkl' , 'yyy \\', 'zzz']
>>>
>>> def join_lines(sequence):
...   i = iter(sequence)
...   buff = ''
...   try:
...     while True:
...       line = i.next()
...       if line.endswith('\\'):
...         line = line[:-1]
...         buff += line
...       else:
...         if buff:
...           buff += line
...           yield buff
...           buff = ''
...         else:
...           yield line
...   except StopIteration:
...     if buff:
...       yield buff
...
>>> print sample
['abc', 'def \\', 'ghi \\', 'jkl', 'yyy \\', 'zzz']
>>> print list(join_lines(sample))
['abc', 'def ghi jkl', 'yyy zzz']