如何用python找到txt文件中指定内容

Pope2014 2019-08-25 06:38:52
TXT文件中部分内容如下:
3.981E+00 3.288E-08 26.304|*********|*********|****** | | | | | | | |
5.012E+00 4.443E-08 35.551|mmmmmmmmm|mmmmmmmmm|mmmmmmmmm|mmmmmm | | | | | | |
6.310E+00 5.748E-08 45.989|*********|*********|*********|*********|****** | | | | | |
7.943E+00 7.110E-08 56.893|*********|*********|*********|*********|*********|******* | | | | |
1.000E+01 8.298E-08 66.392|*********|*********|*********|*********|*********|*********|****** | | | |
1.259E+01 9.413E-08 75.316|*********|*********|*********|*********|*********|*********|*********|***** | | |
1.585E+01 1.019E-07 81.558|*********|*********|*********|*********|*********|*********|*********|*********|** | |
1.995E+01 1.093E-07 87.427|*********|*********|*********|*********|*********|*********|*********|*********|******* | |
2.512E+01 1.165E-07 93.178|*********|*********|*********|*********|*********|*********|*********|*********|*********|*** |
3.162E+01 1.203E-07 96.227|*********|*********|*********|*********|*********|*********|*********|*********|*********|****** |
3.981E+01 1.224E-07 97.946|*********|*********|*********|*********|*********|*********|*********|*********|*********|******** |
5.012E+01 1.242E-07 99.362|*********|*********|*********|*********|*********|*********|*********|*********|*********|*********|
6.310E+01 1.242E-07 99.362|*********|*********|*********|*********|*********|*********|*********|*********|*********|*********|
7.943E+01 1.242E-07 99.362|*********|*********|*********|*********|*********|*********|*********|*********|*********|*********|
1.000E+02 1.250E-07 100.000|*********|*********|*********|*********|*********|*********|*********|*********|*********|*********|
total 1.24979E-07 100.000:--------10--------20--------30--------40--------50--------60--------70--------80--------90-------100
1tally 42 nps = 3574556
tally type 2 particle flux averaged over a surface.
particle(s): neutron
number of histories used for normalizing tallies =************

this tally is modified by a dose function.

this tally is all multiplied by 1.00000E-12

areas
surface: 30
7.85398E+01

surface 30
energy
1.0000E-02 2.38783E-21 0.0290
2.0000E+01 2.51935E-22 0.0524

total 2.63977E-21 0.0284
1analysis of the results in the tally fluctuation chart bin (tfc) for tally 42 with nps = 3574556 print table 160

我想要的是蓝色部分的数据,关键字是红色部分。我想先找到红色部分所在的行的行号,然后加上10,就是数据所在的行的行号(他们之间的间距是固定的),然后开始读取数据,通过判断total来决定读取是否结束(数据的下一行开头是total)。
求助各位大神,这是我第一次用python,拜托拜托了!
...全文
1261 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_45240069 2022-04-01
  • 打赏
  • 举报
回复

哈哈哈太真实了,不会都是搞这个的吧

赵子凡 2020-02-22
  • 打赏
  • 举报
回复
哈哈哈,我也在找用Python读MCNP文件的方法,没想到能看到MCNP的输出文件! 你也是中核单位下的同事吗?
Pope2014 2019-11-14
  • 打赏
  • 举报
回复
确实是MCNP
尐东东 2019-09-20
  • 打赏
  • 举报
回复
直接用 find 然后记录一下行号就好了,

with open('test.txt', 'r') as f:
    context = f.readlines()

target_line = 0
for l in context:
    if l.find('1tally  42        nps') != -1:
        break
    target_line += 1

print(context[target_line+15])
print(context[target_line+16])
输出:

    1.0000E-02   2.38783E-21 0.0290

    2.0000E+01   2.51935E-22 0.0524
mastlw 2019-09-19
  • 打赏
  • 举报
回复
你这是,MCNP?

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧