python - 在python中切片具有相似开始和结束字符串的文本文件的一部分
问题描述
我想切片以下文本
Per MPI rank memory allocation (min/avg/max) = 7.017 | 13.33 | 32.25 Mbytes
Step Temp TotEng KinEng PotEng Fmax Press
0 500 -130649.32 1490.405 -132139.72 460.81189 -300.90016
100 341.08362 -130532.82 1016.7055 -131549.53 386.15965 -2581.88
Loop time of 36971.3 on 4 procs for 200 steps with 1001 atoms
Per MPI rank memory allocation (min/avg/max) = 7.018 | 13.34 | 32.31 Mbytes
Step Temp TotEng KinEng PotEng Fmax Press
300000 488.81974 -134278.39 1457.0788 -135735.47 365.77279 -499.63638
300100 497.0212 -134247.19 1481.5258 -135728.72 239.86708 550.74065
Loop time of 36971.3 on 4 procs for 200 steps with 1001 atoms
它应该如下所示
Step Temp TotEng KinEng PotEng Fmax Press
0 500 -130649.32 1490.405 -132139.72 460.81189 -300.90016
100 341.08362 -130532.82 1016.7055 -131549.53 386.15965 -2581.88
300000 488.81974 -134278.39 1457.0788 -135735.47 365.77279 -499.63638
300100 497.0212 -134247.19 1481.5258 -135728.72 239.86708 550.74065
或者
Step Temp TotEng KinEng PotEng Fmax Press
0 500 -130649.32 1490.405 -132139.72 460.81189 -300.90016
100 341.08362 -130532.82 1016.7055 -131549.53 386.15965 -2581.88
Step Temp TotEng KinEng PotEng Fmax Press
300000 488.81974 -134278.39 1457.0788 -135735.47 365.77279 -499.63638
300100 497.0212 -134247.19 1481.5258 -135728.72 239.86708 550.74065
也就是说,我想从“Step”的开头切片到“Loop”字符串,这些字符串在上述格式的文本中多次出现。我试着用
start_str = 'Step'
end_str = 'Loop'
f = open("log.lammps","r").read()
lines = f [ f.find(start_str) : f.find(end_str) ]
print(lines)
但它只打印前半部分(Step to Loop)并停止。
解决方案
您可以使用re
模块来完成任务:
import re
with open('log.lammps', 'r') as f_in:
s = f_in.read()
all_data = []
for part in re.findall(r'^Step.*?\n(.*?)\n^Loop', s, flags=re.S|re.M):
for line in part.splitlines():
all_data.append(line.split())
print(('{:<12}'*7).format('Step', 'Temp', 'TotEng', 'KinEng', 'PotEng', 'Fmax', 'Press'))
for row in all_data:
print(('{:<12}'*7).format(*row))
印刷:
Step Temp TotEng KinEng PotEng Fmax Press
0 500 -130649.32 1490.405 -132139.72 460.81189 -300.90016
100 341.08362 -130532.82 1016.7055 -131549.53 386.15965 -2581.88
300000 488.81974 -134278.39 1457.0788 -135735.47 365.77279 -499.63638
300100 497.0212 -134247.19 1481.5258 -135728.72 239.86708 550.74065
或者放入all_data
一个DataFrame:
import pandas as pd
df = pd.DataFrame(all_data, columns=['Step', 'Temp', 'TotEng', 'KinEng', 'PotEng', 'Fmax', 'Press'])
print(df)
印刷:
Step Temp TotEng KinEng PotEng Fmax Press
0 0 500 -130649.32 1490.405 -132139.72 460.81189 -300.90016
1 100 341.08362 -130532.82 1016.7055 -131549.53 386.15965 -2581.88
2 300000 488.81974 -134278.39 1457.0788 -135735.47 365.77279 -499.63638
3 300100 497.0212 -134247.19 1481.5258 -135728.72 239.86708 550.74065
推荐阅读
- git - Git:试图在已删除的分支上追溯压缩的提交 - 有可能吗?
- angularjs - this.router.navigate 在登录时第二次不工作
- python - 如何在 Django 表单中设置默认值?
- r - 使用一般线性模型 (GLM) 后的事后检验
- vb.net - REDIM PRESERVE 是否将所有新元素设置为 NOTHING?
- java - 在 android 上,声明一个单例类来管理 socket.io 连接会是一个错误吗?
- php - 在 Laravel 中为新行添加 div 行
- php - 如何将数组键传递给 PHP 中的数组
- r - 使用 GAMLSS 进行似然比测试中的错误消息
- python - 带有可选组的 Python 正则表达式