使用python next()和strip()检索以下行

时间:2014-01-15 03:03:37

标签: python lines next strip

我在使用next()strip()检索该行之后的行时遇到问题。测试数据看起来像这样:

@abcde:111/2
ABCDEFGHIj
+abcde:111/2
bla11
@abcde:115/2
JDIJSKNDIJ
+abcde:115/2
bla13
@abcde:113/2
djijwkoken
+abcde:113/2
bla15

我的目标是删除所有以' @'开头的4行。包含' N'在第二行。预期的测试输出应如下所示:

@abcde:111/2
ABCDEFGHIj
+abcde:111/2
bla11
@abcde:113/2
djijwkoken
+abcde:113/2
bla15

这是我的代码(delete_N.py),我在远程Ubuntu服务器上使用Mac OS终端运行它,使用python 2.7:

import sys

filename1 = sys.argv[1] #file to process

data = open(filename1, 'r')

def del_N(input1):
    for line in input1:
        if line[:1] == '@' and 'N' not in next(input1).strip():
            print line.strip()
            for i in range(3):
                print next(input1).strip()

del_N(data)

但是我收到以下错误:

Traceback (most recent call last):
  File "delete_N.py", line 14, in <module>
    del_N(data)
  File "delete_N.py", line 12, in del_N
    print next(input1).strip()
StopIteration

我做错了什么?

3 个答案:

答案 0 :(得分:3)

在您的程序中,您正在阅读文件中的数据。检查Lego's answer,他非常清楚地解释了这个错误。

你可以这样做。该程序假定文件中的行数是4的倍数。

with open("Input.txt", "r") as input_file:
    for line1 in input_file:
        line2, line3, line4 = [next(input_file) for _ in xrange(3)]
        if "N" not in line2:
            print line1 + line2 + line3 + line4.rstrip()

<强>输出

@abcde:111/2
ABCDEFGHIj
+abcde:111/2
bla11
@abcde:113/2
djijwkoken
+abcde:113/2
bla15

答案 1 :(得分:2)

当你到达迭代器的末尾时,Python会引发StopIteration异常。如果您手动在迭代器上调用next(),而不是使用for ... in ...循环(将在StopIteration引发时终止),则必须捕获{ {1}}并处理它,因为它意味着......好吧,迭代器已经停止了。


无论如何,这是一个(IMO)清洁解决方案:

StopIteration

结果:

data = ... # your data goes here, from a file or whatever
lines = data.split('\n')
n = 4
groups = zip(*[lines[i::n] for i in range(n)])
# or, groups = zip(lines[0::4], lines[1::4], lines[2::4], lines[3::4])
result = []

for group in groups:
    if group[0].startswith('@') and 'N' in group[1]:
        continue # i.e. don't append
    else:
        result.append(group)

joined_result = '\n'.join(['\n'.join(group) for group in result])
print(joined_result)

答案 2 :(得分:2)

问题在于,在使用for循环遍历文件的同时,next也会在光标移动文件时迭代光标。这意味着对于每次迭代,您实际上一次跳过 3 点。

例如,请查看此文件:

                openning the file
@abcde:111/2    for line in input1: # First iteration.
ABCDEFGHIj          if line[:1] == '@' and 'N' not in next(input1).strip():
+abcde:111/2            print next(input1).strip()
bla11           for line in input1: # Second iteration.
@abcde:115/2       etc...

查看每次迭代如何跳转最多3行,因此当遇到迭代中的倒数第二行或最后一行时,它将溢出并引发StopIteration错误。

相关问题