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



我被这个卡住了:

当它在第二个文件中找到"/MAT/LAW2/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文件有200000行(:

/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。

我受到了这个主题的启发:在枚举列表对象(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

分离器目前不是问题(我知道怎么做(。

最后,我想要一个文本B,就像文本a的片段。

有人能帮我吗?

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

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

对于类似于您的案例的问题,主要且著名的算法之一是功能切换方法;只需设置变量来检查何时应该在文件中写入,何时不应该写入,请尝试以下操作:

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

相关内容

  • 没有找到相关文章

最新更新