2017-05-03 9 views
-5

は、ファイル出力一致文字列と以下

xyz abc 
abc xyz 
apple orranges fruits 
train bus flight 
     airbus greatbus 
vegetables not in place. 

である私は、パターン「電車・バスの便」を見つけると、上記のすべての行は、電車・バスの便

を含める削除する必要がpythonでマッチした文字列までの行を削除出力は:

 airbus greatbus 
vegetables not in place. 

誰でもお勧めしますか。

おかげ

+1

あなたは何を試しましたか?あなたのコードとあなたが直面している問題を教えてください – kuro

答えて

0

ただ、各LINかどうかを確認eにはあなたが探しているテキストが含まれています。

# Assuming the input file is called "input.txt" 
with open('input.txt', 'r') as fin: 
    # Read all the lines 
    buff = iter(fin.readlines()) 

# For the output file do the following 
with open('output.txt', 'w') as fout: 
    # Iterate over every line 
    for line in buff: 
    # Check if the text you look for is not in the line 
    if "train bus flight" not in line: 
     # If not found check next line 
     continue 
    else: 
     # Another for loop to start from where you are 
     for line in buff: 
     # Write the rest of the lines 
     fout.write(line) 
0

あなたはすべての3つは、その行の任意の位置に言葉を述べた行を削除しますか?なぜ xyz abcabc xyz行が削除されたのか分かりません。これらにはtrain bus flightがありません。

次に、これを行う方法があります。

Pythonの3ソリューション:

with open("a.txt","r") as fp: 
    line_list = fp.readlines() 
    for line in line_list: 
     if all(word in line for word in ["train", "bus", "flight"])==False: 
      print(line[:-1]) 

出力:

xyz abc 
abc xyz 
apple orranges fruits 
     airbus greatbus 
vegetables not in place 

A.TXT:

xyz abc 
abc xyz 
apple orranges fruits 
train bus flight 
     airbus greatbus 
vegetables not in place. 
+0

私は文字列までのすべての行を削除したいと思います。それらが削除された理由です。 – gopinara

+0

私は質問を修正しました – gopinara

関連する問題