2016-07-14 2 views
1

まず間の読み取りは、私の例のテキストファイルの内容は次のようになります。テキストファイルの行

Some Data 
Nothing important 
Start here 
This is important 
Grab this line too 
And this ono too 
End here 
Text goes on, but isn't important 
Next text 
Blaah 

そして今、私はテキストファイルで読みたい、と私は唯一のラインをつかむしたいです「ここから開始」と「ここに終了」の間にあります。

だから私のPythonのコードは次のようになります。

filename = 'example_file.txt' 

with open(filename, 'r') as input: 
    for line in input: # First loop breaks at specific line 
     if 'Start here' in line: 
      break 

    for line_1 in input: # Second loop grabs all lines 
     print line_1.strip() 

    for line_2 in input: # Third loop breaks at specific line 
     if 'End here' in line_2: 
      break 

しかし、それは動作しません。ここで

私の出力、私はそれを実行します。あなたが見ることができるように

This is important 
Grab this line too 
And this on too 
End here 
Text goes on, but isn't important 
Next text 
Blaah 

、私のスクリプトはここエンドで中断されません。プログラムは正しい行から始まりますが、正しい行では途切れません。

どういうところが間違っていますか?

答えて

3

それはあなたが最初にすべての行を読んで、それを列挙することができます...

for line_1 in input: 
    if 'End here' in line_1: 
     break 
    print line_1.strip() 
0

re.DOTALLオプションの正規表現(reモジュール)を使用して、改行を通常の文字と見なすことができます。それが動作する理由

import re 

source = """Some Data 
Nothing important 
Start here 
This is important 
Grab this line too 
And this ono too 
End here 
Text goes on, but isn't important 
Next text 
Blaah""" 

# or else: 
# source = open(filename, 'r').read() # or similar 

result = re.search("Start here(.*)End here", source, re.DOTALL).group(1).strip() 

print result 

> This is important 
> Grab this line too 
> And this ono too 

  • re.searchは、いくつかの文字列のパターンを探します。
  • 括弧はグループでマッチを区切ります。最初のグループはパターン全体、2番目のグループはカッコです。グループはシーケンスとネストすることができます。
  • .*は、「任意のチャート、任意の回数」を意味します。 2つのハードコードされたマーカー(つまりStart HereEnd here)の間にすべてを取る必要があります。
  • re.DOTALLは秘密です:改行文字を通常の文字列文字として扱います。ドットは「任意の文字」のシンボルなので、「すべてドット」は「任意の文字を通常の文字、改行文字でも扱う」ことを意味します。
  • group(1)は、括弧の中にある2番目の(ゼロから始まるインデックス)グループを必要とすることを意味します。
+0

TAのheltonbikerを、あなたのソリューションの作品を。しかし、私はPython初心者です。 「グループ(1)」は何を説明してくれますか?なぜグループで1番?そして、私はこの表現 "。*"が私が行間を読むことができる理由だと思います、そうですか? – Sophus

+0

私の編集をご覧ください。 – heltonbiker

0

ブレークを必要とする第二のループです:

filename = 'example_file.txt' 

useful_content = [] 
with open(filename, 'r') as input: 
    all_lines = input.readlines() # read all lines 
    for idx in range(len(all_lines)): # iterate all lines 
    if 'Start here' in all_lines[idx]: 
     useful_content.append(all_lines[idx].strip()) 
     idx = idx + 1 
     # found start of useful contents, continue iterate till it ends 
     while 'End here' not in all_lines[idx]: 
      useful_content.append(all_lines[idx].strip()) 
      idx = idx + 1 
     break 
for line in useful_content: 
    print(line) 
+0

TA Chong Tang、しかし私はあなたの解決策が問題につながると思います。テキストファイルが非常に大きいとします。あなたのソリューションでは、内容全体がメモリ(RAM)に書き込まれます。 – Sophus

0

あなたの問題はあなたがチェックしなければならないということです2番目と3番目が同時に実行されないので、2番目のループで 'ここに終了'実際、3番目のループは実行されません。

を念頭に置いて

、このコードは動作します:

filename = 'mydata.txt' 

with open(filename, 'r') as f: 
    for line in f: 
     if 'Start here' in line: 
      break 

    for line_1 in f: 
     if 'End here' in line: 
      break 
     else: 
      print line.strip() 

は、しかし、私たちが作ることができるいくつかの最適化はまだあります:ループの上

  • 変数は、ループのみの場合とローカルであり、私たちはその名前を再利用することができます。
  • breakの後のコードは、とにかく実行されないので、elseを取り除くことができます。
  • openは、デフォルトで読み取りモードを使用します。これを考慮して

、あなたの最終的なコードは次のようになります。あなたが所望の出力を得るだろう、と

filename = 'mydata.txt' 

with open(filename) as f: 
    for line in f: 
     if 'Start here' in line: 
      break 

    for line in f: 
     if 'End here' in line: 
      break 
     print line.strip() 

ラン:

This is important 
Grab this line too 
And this ono too 
+0

TA pta2002、あなたは正しいです。 :-) – Sophus

関連する問題