2017-07-05 4 views
0

〜43k行のtxtファイルを変更しようとしています。コマンド* Nsetがファイルに与えられた後、そのコマンドに続くすべての行を抽出して保存し、ファイル内の次の*コマンドに到達すると停止する必要があります。各コマンドの後には、異なる数の行と文字があります。例えば、ここでファイルのサンプル一部です:ファイル間で見出しの間の行を抽出する

*Nset 

1, 2, 3, 4, 5, 6, 7, 

12, 13, 14, 15, 16, 

17, 52, 75, 86, 92, 

90, 91, 92 93, 94, 95.... 

*NEXT COMMAND 

blah blah blah 

*Nset 

numbers 

*Nset 

numbers 

*Command 

irrelevant text 

コード私は必要な数字が2 *数Nsetの間に存在しないとき、私は現在、作品を持っています。 1つの* Netsが他の番号に続くとき、それはそのコマンドと進行する行を一緒にスキップして、なぜ私は理解できません。次のコマンドが* Nsetでなければ、次のコマンドを見つけて、データを完璧に引き出す。

import re 

# read in the input deck 
deck_name = 'master.txt' 
deck = open(deck_name,'r') 

#initialize variables 
nset_data = [] 
matched_nset_lines = [] 
nset_count = 0 

for line in deck: 
    # loop to extract all nset names and node numbers 
    important_line = re.search(r'\*Nset,.*',line) 
    if important_line : 
     line_value = important_line.group() #name for nset 
     matched_nset_lines.insert(nset_count,line_value) #name for nset 
     temp = [] 

     # read lines from the found match up until the next *command 
     for line_x in deck : 
      if not re.match(r'\*',line_x): 
       temp.append(line_x) 
      else : 
       break 

     nset_data.append(temp) 

    nset_count = nset_count + 1 

私はPython 3.5を使用しています。助けてくれてありがとう。

+0

'* *'で始まる行の先頭には、常に*コマンドがありますか? –

+0

@ juanpa.arrivillaga、はい。いろいろなコマンドがありますが、それぞれが "*"の直前です。そして次の行は数字です。 –

+0

これはまったく関係ありますか? https://stackoverflow.com/questions/25943000/finding-a-word-between-two-words-that-will-not-match-if-the-closing-word-occurs –

答えて

0

あなただけの以下のアプローチが動作するはず*Nsets間に線を抽出したい場合:

In [5]: with open("master.txt") as f: 
    ...:  data = [] 
    ...:  gather = False 
    ...:  for line in f: 
    ...:   line = line.strip() 
    ...:   if line.startswith("*Nset"): 
    ...:    gather = True 
    ...:   elif line.startswith("*"): 
    ...:    gather = False 
    ...:   elif line and gather: 
    ...:    data.append(line) 
    ...: 

In [6]: data 
Out[6]: 
['1, 2, 3, 4, 5, 6, 7,', 
'12, 13, 14, 15, 16,', 
'17, 52, 75, 86, 92,', 
'90, 91, 92 93, 94, 95....', 
'numbers', 
'numbers'] 

そして、あなたは追加情報が必要な場合、上記を拡張するために十分に簡単です:

In [7]: with open("master.txt") as f: 
    ...:  nset_lines = [] 
    ...:  nset_count = 0 
    ...:  data = [] 
    ...:  gather = False 
    ...:  for i, line in enumerate(f): 
    ...:   line = line.strip() 
    ...:   if line.startswith("*Nset"): 
    ...:    gather = True 
    ...:    nset_lines.append(i) 
    ...:    nset_count += 1 
    ...:   elif line.startswith("*"): 
    ...:    gather = False 
    ...:   elif line and gather: 
    ...:    data.append(line) 
    ...: 

In [8]: nset_lines 
Out[8]: [0, 14, 18] 

In [9]: nset_count 
Out[9]: 3 

In [10]: data 
Out[10]: 
['1, 2, 3, 4, 5, 6, 7,', 
'12, 13, 14, 15, 16,', 
'17, 52, 75, 86, 92,', 
'90, 91, 92 93, 94, 95....', 
'numbers', 
'numbers'] 
0

これはあなたが望むことをします。

command = [] 
commandLines = [] 

with open('test.txt') as file: 
    for line in file: 
     if line.startswith('*'): 
      command.append(line.rstrip()) 
      commandLines.append([]) 
     else: 
      commandLines[-1].append(line.rstrip()) 

import pprint 

pprint.pprint(command) 
pprint.pprint(commandLines) 

commandLines[i]command[i]に対応する行を含むリストです。コマンドの

プリントアウト:

['*Nset', '*NEXT COMMAND', '*Nset', '*Nset', '*Command'] 

そしてライン(ネストされたリスト):

[['1, 2, 3, 4, 5, 6, 7,', 
    '12, 13, 14, 15, 16,', 
    '17, 52, 75, 86, 92,', 
    '90, 91, 92 93, 94, 95....'], 
['blah blah blah'], 
['numbers'], 
['numbers'], 
['irrelevant text']] 

仮定:のみのコマンドラインは '*' で始まります。

関連する問題