如何在其他循环中继续 enumerate() 循环

How to continue enumarete() loop in other loop

我坚持这个:

当它在第二个文件中找到要写入的“/MAT/LAW02/1”时,我想从我的文件中获取未来的 12 行。

之后,我希望它一直分析到最后。

但是我卡住了,因为我找不到关于这个问题的主题。

这是我当前的代码:

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        for i, line in enumerate(textFile):
            if '/MAT/LAW02/1' in line:
                catchInfo = line.strip().split()
                toString = ''.join(catchInfo)
                textFileClean.write(toString)
    textFile.close()
textFileClean.close()

这是我要提取的 textA 文件的片段(因为 textA 文件有 200,000 行):

/MAT/LAW02/1
ES_ODG2_MED-5                                                                                      
#                RHO|           REF. RHO|
             7.82E-9
#                  E|                  v|
             210000.                 0.3
#                  a|                  b|                  n|               emax|               smax|
               273.1               437.6               0.724               1.E30               1.E30
#                  c|                 e0|      ICC|  Fsmooth|               Fcut|              Chard|
               0.097                0.32         1         0               1.E30
#                  m|              Tmelt|             rho0Cp|                 Ti|
                  0.                  0.                  0.                298.

这是我在 运行 上面代码之后的 textB 文件:

/MAT/LAW02/1

我想到了这样的事情:

from itertools import islice

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        it = iter(enumerate(textFile))
        for i, line in it:
            x = 0
            y = 12
            if '/MAT/LAW02/1' in line:
                while x != y:
                    catchInfo = line.strip().split()
                    toString = ''.join(catchInfo)
                    textFileClean.write(toString)
                    place_where_skip_happened = i
                    next(islice(it, 1, 1), None)
                    x += 1
    textFile.close()
textFileClean.close()

我想从 1 乘 1 到 12。

我受到这个话题的启发:

但这对我不起作用。

这是 运行 此代码后的我的 textB 文件:

/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1

分隔符目前不是问题(我知道怎么做)。

最后我想要一个类似 textA 片段的 textB。

有人可以帮助我吗?

也许做你想做的最简单的方法是顺序使用循环

from itertools import islice

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        it = iter(textFile)
        for line in it:
            if '/MAT/LAW02/1' in line:  # once it's found, you exit the loop
                break
        for i, line in enumerate(it):
            if i > 12:
                break
            # here do your copy stuff

#    textFile.close()
# textFileClean.close()
# No need to close the file, it's done automatically when you exit the `with`statement

也许保留原来的循环,但为 12 行添加一个计数器,并允许 if 块在行有匹配项或计数器不为零时执行:

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        counter = 0
        for i, line in enumerate(textFile):
            if '/MAT/LAW02/1' in line:
                counter = 13  # the line itself plus the 12 that follow 
            if counter > 0:
                counter -= 1 
                catchInfo = line.strip().split()
                toString = ''.join(catchInfo)
                textFileClean.write(toString)

如果在几行(少于 12 行)内出现两个匹配项,这也会更好地工作。这会将输出扩展到第二个匹配项之后的 12 行。

对于像您的情况一样的类似问题,主要和著名的算法之一是 Feature toggle 方法;只需设置变量来检查我们何时应该写入文件以及何时不应该写入文件,试试这个:

阅读更多内容: https://en.wikipedia.org/wiki/Feature_toggle

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"


header = '/MAT/LAW02/1'
catch_lines_toggle = False
lines_counter = 0
max_lines = 20
with open(inputRadFile, "r") as textFile:
    for each_line in textFile:
        if header in each_line:
            catch_lines_toggle = True

        if catch_lines_toggle is True:
            with open(outputRadFile, "a") as textFileClean:
                catchInfo = each_line.strip().split()
                toString = ''.join(catchInfo)
                textFileClean.write(toString+"\n")

            lines_counter += 1
            if lines_counter == max_lines:
                catch_lines_toggle = False
                break

Python 方式(没有内存效率 - 适用于小文件):

with open("Configs.py", "r") as textFile:
    data = textFile.read()

target_lines = ["".join(each_line.strip().split()) for each_line in data.split('/MAT/LAW02/1')[1].split("\n")[0:13]]

with open("demo/textB.txt", "w") as output:
    output.write("\n".join(target_lines))