【问题标题】:How to continue enumarete() loop in other loop如何在其他循环中继续枚举()循环
【发布时间】:2021-04-06 09:21:38
【问题描述】:

我被这个困住了:

当它找到 '/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 文件的 sn-p(因为 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 到 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

分隔符暂时没有问题(我知道怎么做)。

最后我想要一个像 textA 的 sn-p 一样的 textB。

有人可以帮助我吗?

【问题讨论】:

    标签: python python-3.x iterator enumerate


    【解决方案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(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
    
    

    【讨论】:

    • 感谢您的帮助
    【解决方案2】:

    也许保留原始循环,但为 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 行。

    【讨论】:

    • 太棒了!非常感谢。
    【解决方案3】:

    对于与您的案例类似的问题,主要和著名的算法之一是 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
    

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

    【讨论】:

    • @Ziathes yw :) 如果您也支持我的回答,我将不胜感激。
    猜你喜欢
    • 2021-03-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多