如何同时循环两次遍历文本文件

时间:2019-05-23 09:26:55

标签: python

我打开一个文本文件,寻找以1开头的行。 找到之后,我需要在接下来的6行中进行计算。 现在,我需要继续找到开头为1的下一行。

lcuLogIn="C:\\Automation\\LcuLogs\\LCU_Log_6Ant_Test.log"
with open(lcuLogIn, 'r+') as f:
        for i, line in enumerate(t):
            if line[0] == "1":
                local_counter = 0
                while local_counter < 6:
                    if local_counter == 0:
                        print "do something"
                    if local_counter == 1:
                        print "do something"
                    if local_counter == 2:
                        print "do something"
                    if local_counter == 3:
                        print "do something"
                    if local_counter == 4:
                        print "do something"
                    if local_counter == 5:
                        print "do something"
                    local_counter += 1
                i += 6

索引没有进行,因为它是循环索引。 我不能重复两次-我收到一个错误“混合的迭代会导致数据丢失”

4 个答案:

答案 0 :(得分:0)

我假设t是行列表

lcuLogIn="C:\\Automation\\LcuLogs\\LCU_Log_6Ant_Test.log"
with open(lcuLogIn, 'r+') as f:
    i = 0
    while i < len(t):
        line = t[i]
        if line[0] == "1":
            local_counter = 0
            while local_counter < 6:
                if local_counter == 0:
                    print "do something"
                if local_counter == 1:
                    print "do something"
                if local_counter == 2:
                    print "do something"
                if local_counter == 3:
                    print "do something"
                if local_counter == 4:
                    print "do something"
                if local_counter == 5:
                    print "do something"
                local_counter += 1
            i += 6
        i += 1

答案 1 :(得分:0)

假设您要遍历文件(tf),则可以使用next函数在循环中推进文件迭代器。

lcuLogIn="C:\\Automation\\LcuLogs\\LCU_Log_6Ant_Test.log"
with open(lcuLogIn, 'r+') as f:
        for line in f:
            if line.startswith("1"):
                for i in range(6):
                    # Advance file iterator
                    line = next(f)
                    if i == 0:
                        print "do something"
                    if i == 1:
                        print "do something"
                    if i == 2:
                        print "do something"
                    if i == 3:
                        print "do something"
                    if i == 4:
                        print "do something"
                    if i == 5:
                        print "do something"

如果t实际上是其他可迭代的对象,则可以使用iter函数创建一个迭代器,并以相同的方式使用它。

lcuLogIn="C:\\Automation\\LcuLogs\\LCU_Log_6Ant_Test.log"
with open(lcuLogIn, 'r+') as f:
        it = iter(t)
        for line in it:
            if line.startswith("1"):
                for i in range(6):
                    # Advance the iterator
                    line = next(it)
                    if i == 0:
                        print "do something"
                    if i == 1:
                        print "do something"
                    if i == 2:
                        print "do something"
                    if i == 3:
                        print "do something"
                    if i == 4:
                        print "do something"
                    if i == 5:
                        print "do something"

答案 2 :(得分:0)

如果文件不是很大,则将文件中的行保存在readlines()中,并增加长度限制。

lcuLogIn="C:\\Automation\\LcuLogs\\LCU_Log_6Ant_Test.log"
with open(lcuLogIn, 'r+') as f:
  lines = f.readlines()
  total_lines = len(lines)
  for i, line in enumerate(lines):
    if line.startswith("1"):
      local_counter = 0
      while local_counter < 6 and (i + local_counter < total_lines):
          print(line, local_counter)
          if local_counter == 0:
              print "do something"
          if local_counter == 1:
              print "do something"
          if local_counter == 2:
              print "do something"
          if local_counter == 3:
              print "do something"
          if local_counter == 4:
              print "do something"
          if local_counter == 5:
              print "do something"
          local_counter += 1

答案 3 :(得分:0)

如果您喜欢更优雅的方法,则可以执行以下操作。 for中的log是一个列表,将以相同顺序包含以“ 1”开头的行作为其第一个元素,并以其后6行作为其他元素。尝试用文件更改input_file变量,然后运行它以查看输出。

from itertools import izip_longest

input_file = 'input_file.txt'
lines = open(input_file).readlines()

for log in filter(lambda x: x[0].startswith("1"), izip_longest(*[lines[i:] for i in range(7)])):
    print log[0]
    print '\t', log[1:]

在上面的代码中,您将必须编写关于在for循环内以“ 1”开头的行之后的六行进行处理的逻辑。