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

问题描述

我一直坚持这个:

当它找到“/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,"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 到 12。

我受到了这个话题的启发:Skip iterations in enumerated list object (python)

但它对我不起作用。

这是我运行此代码后的 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

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

最后我想要一个 textB 就像 textA 的片段。

有人可以帮助我吗?

解决方法

也许最简单的方法是按顺序使用循环

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,"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 行。

,

对于与您的情况类似的问题,主要和著名的算法之一是功能切换方法;只需设置变量来检查我们什么时候应该写入文件,什么时候不应该,试试这个:

阅读更多信息: 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

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

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))

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...