0

Python を使用して、モデルからの出力ファイルを後処理する必要があります。出力ファイルには、データと文字列の組み合わせが含まれます。まず、文字列をデータから分離し、各出力時間 (データのみ、文字列なし) の列 0、1、および 2 を別のテキスト ファイルに保存します。したがって、以下の例では、3 つのテキスト ファイル (時間 = 0、時間 = 0.01、時間 = 0.04) があり、それぞれにヘッダーやその他の文字列が含まれていない各出力時間のデータが含まれています。モデルからの出力ファイルの短い形式は次のようになります。

 ******* Program ******  
 ******* Program ******  
 ******* Program ******                                                   
 Date:  26. 4.    Time:  15:40:32
 Units: L = cm   , T = days , M = mmol 


 Time:     0.000000

 Node    Depth    Head   Moisture   HeadF  MoistureF      Flux   
      [L]      [L]     [-]       [L]       [-]       [L/T]   

   1     0.00   -1000.00 0.1088    -1000.00 0.002508  -0.562E-03 
   2    -0.04   -1000.00 0.1088    -1000.00 0.002508  -0.562E-03 
   3    -0.08   -1000.00 0.1088    -1000.00 0.002508  -0.562E-03 
end


 Time:     0.010000

 Node    Depth    Head   Moisture   HeadF  MoistureF      Flux   
          [L]      [L]     [-]       [L]       [-]       [L/T]   

   1     0.00    -666.06 0.1304      -14.95 0.139033  -0.451E-02 
   2    -0.04    -666.11 0.1304      -15.01 0.138715  -0.887E-02 
   3    -0.08    -666.35 0.1304      -15.06 0.138394  -0.174E-01 
end


 Time:     0.040000

 Node    Depth    Head   Moisture   HeadF  MoistureF      Flux    
          [L]      [L]     [-]       [L]       [-]       [L/T]    

   1     0.00    -324.87 0.1720      -12.30 0.157799  -0.315E-02  
   2    -0.04    -324.84 0.1720      -12.31 0.157724  -0.628E-02  
   3    -0.08    -324.83 0.1720      -12.32 0.157649  -0.125E-01  
end

以前にstackoverflowに投稿された別の質問から次のコードを見つけました。その質問へのリンクは次のとおりです。 ここにリンクの説明を入力してください

その問題は私のものと非常によく似ています。ただし、問題を解決するために変更するのに問題があります。私の問題に合わせてどのように変更すればよいですか? または、この問題に取り組むために別の戦略を使用する必要がありますか?

def parse_DPT(lines):
    DPT = []
    while lines:
        line = lines.pop(0).lstrip()
        if line == ' ' or line.startswith('*'):
            continue
        if line.startswith('*'):
            lines.insert(0, line)
            break
        data = line.split(' ')
        # pick only columns 0, 1, 2 and
        # convert to appropiate numeric format
        # and append to list for current DPT and step
        DPT.append([int(data[0]), float(data[1]), float(data[2])])
    return DPT

raw = []
with open('NOD_INFTEST.txt') as nit:
    lines = nit.readlines()
while lines:
line = lines.pop(0)

if line.startswith(''):
    if line.find('Time:') > -1:
        raw.append(parse_DPT(lines))

from pprint import pprint
for raw_step in zip(raw):
    print 'raw:'
    pprint(raw_step)

Python から取得したエラー メッセージは次のとおりです。

'import sitecustomize' failed; use -v for traceback
Traceback (most recent call last):
  File "C:\Users\Desktop\python test\p-test3.py", line 58, in <module>
    raw.append(parse_DPT(lines))
  File "C:\Users\Desktop\python test\p-test3.py", line 35, in parse_DPT
    DPT.append([int(data[0]), float(data[1]), float(data[2])])
ValueError: invalid literal for int() with base 10: 'Units:'
4

1 に答える 1

1

私があなたの質問を理解していれば、このコードでうまくいくはずです:

import re

with open('in.txt', 'r') as in_file:
    file_content = in_file.read()
    blocks = re.findall(
        'Time:\s*\d+\.\d*(.*?)end',
        file_content,
        re.DOTALL
        )

    file_number = 1
    for block in blocks:
        with open('out%s.txt'%str(file_number), 'w') as out_file:
            for row in re.findall(
                    '\s*(-?\d+.?\d*)\s*(-?\d+.?\d*)\s*(-?\d+.?\d*).*',
                    block):
                out_file.write(row[0] + ' ' + row[1] + ' ' + row[2] + '\n')
        file_number += 1

コードは、テキストを含むファイルが呼び出されることを前提としていますin.txt

于 2013-05-03T00:40:30.923 に答える