Python在一个全是文本文件的文件夹中搜索多个字符串,复制它下面的行和50行到另一个文件

问题描述 投票:0回答:1

我有一个名为“输入”的文件夹,其中包含以“.measbu”或“.info”扩展名结尾的文本文件(例如,c.171203fl.so.power.simu.measbu 和 s.170210.所以.power.info)

.measbu 文本文件包含如下所示的地图:

 channel averaged  cell avg  fast fluxes   core 171203fl  k-eff: 1.052447 scale = 1e+14

                                    reactor power = 100.0 mw

                     Triad3PC-Power v1.1.1.1   ld=2007-02-01

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                             0.019                                            34
 33                                 0.042   0.054   0.043   0.015                                33
 32                             0.052   0.115   0.079   0.064   0.009                            32
 31                        0.035    0.174   0.158   0.128   0.047   0.003                        31
 30                    0.034    0.170   0.269   0.129   0.160   0.012   0.002                    30
 29                0.020   0.118    0.427   0.377   0.329   0.068   0.004   0.003                29
 28                    0.102    0.331   0.454   0.140   0.324   0.015   0.007                    28
 27                0.053   0.263    0.385   0.301   0.111   0.082   0.009   0.016                27
 26            0.017   0.151    0.509   0.194   0.102   0.100   0.039   0.027   0.019            26
 25        0.006   0.051   0.397    0.379   0.134   0.099   0.203   0.031   0.052   0.011        25
 24            0.024   0.086    0.232   0.288   0.292   0.135   0.051   0.136   0.050            24
 23        0.016   0.029   0.140    0.160   0.203   0.281   0.103   0.088   0.108   0.036        23
 22            0.047   0.096    0.178   0.341   0.402   0.347   0.079   0.249   0.118            22
 21        0.045   0.085   0.487    0.219   0.484   0.226   0.183   0.294   0.362   0.070        21
 20    0.017   0.132   0.215    0.683   0.192   0.470   0.482   0.197   0.173   0.143   0.032    20
 19        0.087   0.374   0.442    0.493   0.191   0.206   0.511   0.358   0.188   0.142        19
 18    0.030   0.204   0.532    0.504   0.147   0.193   0.212   0.478   0.248   0.424   0.054    18
 17        0.104   0.365   0.324    0.144   0.361   0.378   0.245   0.717   0.469   0.185        17
 16    0.031   0.205   0.185    0.104   0.135   0.238   0.217   0.577   0.466   0.302   0.061    16
 15        0.090   0.357   0.096    0.090   0.458   0.269   0.513   0.569   0.203   0.173        15
 14    0.023   0.137   0.148    0.097   0.192   0.567   0.529   0.232   0.301   0.343   0.052    14
 13        0.053   0.125   0.260    0.332   0.577   0.558   0.351   0.127   0.121   0.134        13
 12    0.012   0.054   0.375    0.116   0.204   0.305   0.264   0.079   0.100   0.122   0.035    12
 11        0.039   0.127   0.251    0.107   0.472   0.524   0.126   0.067   0.147   0.109        11
 10            0.120   0.189    0.056   0.249   0.212   0.420   0.051   0.282   0.311            10
  9        0.044   0.383   0.156    0.063   0.182   0.187   0.116   0.135   0.381   0.096         9
  8            0.146   0.449    0.113   0.104   0.391   0.102   0.189   0.173   0.156             8
  7        0.027   0.227   0.461    0.144   0.292   0.098   0.305   0.558   0.099   0.032         7
  6            0.066   0.231    0.436   0.182   0.125   0.138   0.593   0.170   0.046             6
  5                0.097   0.128    0.483   0.177   0.154   0.242   0.553   0.064                 5
  4                    0.080    0.132   0.501   0.153   0.511   0.218   0.146                     4
  3                0.021   0.046    0.160   0.422   0.475   0.455   0.109   0.030                 3
  2                    0.020    0.054   0.231   0.162   0.208   0.098   0.036                     2
  1                        0.014    0.084   0.167   0.161   0.137   0.026                         1
  0                             0.022   0.091   0.092   0.086   0.034                             0
 -1                                 0.025   0.055   0.052   0.031                                -1
 -2                                             0.021                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

                                         19-DEC-17  10:56:31

.info 文件包含如下所示的地图:

 scan 170210 fission power at tpd 223207.8   total power  112.210

                     Triad3PC-ScanPower v1.1.1.1   ld=2007-03-27

 --------------------------------------------------------------------------------------------------
          burnup type: measbu     lattice split: on     discontinuity factors: normal
 --------------------------------------------------------------------------------------------------

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

 34                                               ...                                            34
 33                                   ...     ...     ...     ...                                33
 32                               ...     ...     ...     ...     ...                            32
 31                          ...      ...     ...     ...     ...     ...                        31
 30                      ...      ...     ...     ...     ...     ...     ...                    30
 29                  ...     ...    1.278   1.450   1.306     ...     ...     ...                29
 28                      ...    0.582   1.391     ...   1.353     ...     ...                    28
 27                  ...   0.597    1.071     ...     ...     ...     ...     ...                27
 26              ...     ...    1.533     ...     ...   0.670     ...     ...     ...            26
 25          ...     ...   1.372    1.019     ...     ...   0.735     ...     ...     ...        25
 24              ...     ...      ...   0.839   0.761     ...     ...   0.725     ...            24
 23          ...     ...     ...      ...     ...   0.966     ...     ...   1.258     ...        23
 22              ...     ...      ...   0.872   1.081   1.050     ...   0.629     ...            22
 21          ...     ...   1.667      ...   1.068     ...     ...   1.058   1.479     ...        21
 20      ...     ...     ...    2.327     ...   1.520   1.811     ...     ...     ...     ...    20
 19          ...   1.385   0.966    1.557   1.282     ...   1.497   0.981     ...     ...        19
 18      ...     ...   1.571    1.059     ...     ...   1.081   1.467     ...   1.298     ...    18
 17          ...   0.777   1.378      ...   1.553   1.694     ...   2.371   1.523     ...        17
 16      ...     ...     ...      ...     ...     ...     ...   1.659   1.237     ...     ...    16
 15          ...   1.342     ...    1.250   1.524     ...   0.969   0.961     ...     ...        15
 14      ...     ...     ...      ...     ...   1.783   1.592     ...   0.888   1.088     ...    14
 13          ...     ...   1.102    1.581   1.778   1.629   1.297     ...   0.149     ...        13
 12      ...     ...   1.596      ...     ...     ...     ...     ...     ...     ...     ...    12
 11          ...     ...   1.323      ...   1.563   1.451     ...     ...   0.156     ...        11
 10              ...     ...      ...   1.550     ...   0.959     ...   0.959   0.509            10
  9          ...   1.297     ...    1.541     ...     ...     ...     ...   0.945     ...         9
  8              ...   1.622      ...     ...   0.851     ...     ...     ...     ...             8
  7          ...     ...   1.535      ...   1.060     ...   0.775   1.396     ...     ...         7
  6              ...     ...    1.595     ...     ...     ...   1.364     ...     ...             6
  5                  ...     ...    1.490   1.038     ...     ...     ...     ...                 5
  4                      ...      ...   1.546     ...   1.280     ...     ...                     4
  3                  ...     ...      ...   1.490   1.488   1.097     ...     ...                 3
  2                      ...      ...     ...     ...     ...     ...     ...                     2
  1                          ...      ...     ...     ...     ...     ...                         1
  0                               ...     ...     ...     ...     ...                             0
 -1                                   ...     ...     ...     ...                                -1
 -2                                               ...                                            -2

          y   z   a   b   c   d    e   f   g   h   j   k   l   m   n   o   p   q   r   s   t 

                                         13-FEB-17  10:04:39

我希望 Python 遍历“输入”文件夹中的每个 .measbu 和 .info 文件并提取地图并将它们写入一个名为“maps.txt”的单独文件。我考虑搜索“频道”和“总功率”,因为这些词/短语出现在每张地图上方(.measbu 文件中的“频道”,.info 文件中的“总功率”),然后复制该行以及下面的 50 行,因为每张地图大约有 50 行长。

到目前为止我的代码:

import os

for filename in os.listdir('inputs'):
    fn = 'inputs/'+filename
    for line in open(fn, 'r'):
        line.find('channel' or 'total power')
        open('maps.txt', 'w').write(line)

但它只是将日期“13-FEB-17 10:04:39”写入 maps.txt,仅此而已,我不确定如何复制并粘贴每个“频道”查找下方的 50 行'或'总功率'。

python string search
1个回答
0
投票

文件中只有一行的原因是每次您以

maps.txt
模式打开
w
文件时,您正在删除它以前的内容并在文档中写入一行。相反,您应该做的是在 for 循环之外打开
maps.txt
,以便在遍历每个目录文件时它保持打开状态,并将每个匹配的文件内容附加到
maps.txt
文件的末尾。

例如:

import os

dirname = 'inputs'
phrases = ["total power", "channel"]

with open('maps.txt', 'w', encoding='utf8') as outfile:
    for filename in os.listdir(dirname):
        fn = os.path.join(dirname, filename)

        with open(fn, 'r', encoding="utf8") as infile:
            line = next(infile)

            if any([phrase in line for phrase in phrases]):
                contents = line + infile.read()
                outfile.write(contents + ("\n" * 2))

补充说明:

  • line.find('channel' or 'total power')
    实际上什么都不做。 表达式
    'channel' or 'total power'
    将始终计算为
    channel
    ,而
    line.find('channel')
    也没有帮助你,因为你没有检查任何返回值。

  • 如前所述,在

    'w'
    模式下打开文档会删除该文档之前包含的任何内容。

  • 最好使用

    with
    语句进行文件处理并明确指定编码。

  • os.path.joins
    是一种更安全、更简单的连接路径部分的方法。

© www.soinside.com 2019 - 2024. All rights reserved.