2016-10-13 4 views
0

テキストファイルに小説を貼り付けました。Python小文字の文字列から単語の完全な文を削除する

"Thermal Molecular Movement in , Order and Probability"

"Molecular and Ionic Interactions as the Basis for the Formation"

"Interfacial Phenomena and Membranes"

私の最初の試みは、通りである: 私は、彼らが(ちょうどこれらの行ではその出現が同様に行います削除)各ページの最上部で発生保つよう、以下の文章を含むすべての行を削除したいです次の次のおもちゃの例では、正常に動作として

mystring = file.read() 
mystring=mystring.strip("Molecular Structure of Biological Systems") 
mystring=mystring.strip("Thermal Molecular Movement in , Order and Probability") 
mystring=mystring.strip("Molecular and Ionic Interactions as the Basis for the Formation") 
mystring=mystring.strip("Interfacial Phenomena and Membranes") 

new_file=open("no_refs.txt", "w") 

new_file.write(mystring) 

file.close() 

は、しかし、これは出力テキストファイルには影響を及ぼさなかった...内容は完全に不変であった...私は、これは奇妙な見つける:

>>> "Hello this is a sentence. Please read it".strip("Please read it") 
'Hello this is a sentence.' 

上記は、私が代わりに次のことを試してみました動作しなかったので:

file=open("novel.txt", "r") 
mystring = file.readlines() 
for lines in mystring: 
    if "Thermal Molecular Movement in , Order and Probability" in lines: 
     mystring.replace(lines, "") 
    elif "Molecular and Ionic Interactions as the Basis for the Formation" in lines: 
     mystring.replace(lines, "") 
    elif "Interfacial Phenomena and Membranes" in lines: 
     mystring.replace(lines, "") 
    else: 
     continue 

new_file=open("no_refs.txt", "w") 

new_file.write(mystring) 
new_file.close() 
file.close() 

しかし、この試みのために私はこのエラーを取得する:

はTypeError:文字列またはその他の文字バッファオブジェクト

答えて

2
    を期待
  • 最初にstr.strip()は、の開始またはの末尾に見つかった場合のみパターンを削除します。は、動作すると思われる文字列あなたのテストでは、実際にはあなたが望むものではありません。
  • 第二に、あなたがいない現在の行にリストに置き換えを実行しようとしている(そして、あなたは交換用の結果をバック割り当てない)
  • ここ

が正常のパターンを削除する修正版です行:

with open("novel.txt", "r") as file: 
    mystring = file.readlines() 
    for i,line in enumerate(mystring): 
     for pattern in ["Thermal Molecular Movement in , Order and Probability","Molecular and Ionic Interactions as the Basis for the Formation","Interfacial Phenomena and Membranes"]: 
      if pattern in line: 
       mystring[i] = line.replace(pattern,"")      

    # print the processed lines 
    print("".join(mystring)) 

注値&インデックスに反復することができenumerate構築物。値だけを反復すると、パターンを見つけることができますが、元のリストでパターンを修正することはできません。

ブロックを離れるとすぐにファイルを閉じるwith open構造にも注意してください。ここ

は完全にパターンを含む行を削除したバージョンは、(いくつかのワンライナー関数型プログラミングのものがそこにあります、上のハングアップ)は次のとおり

with open("novel.txt", "r") as file: 
    mystring = file.readlines() 
    pattern_list = ["Thermal Molecular Movement in , Order and Probability","Molecular and Ionic Interactions as the Basis for the Formation","Interfacial Phenomena and Membranes"] 
    mystring = "".join(filter(lambda line:all(pattern not in line for pattern in pattern_list),mystring)) 
    # print the processed lines 
    print(mystring) 

について説明:状態の係るラインのフィルタリスト:なし不要なパターンのうちの1つがライン内になければなりません。

+0

これは大変感謝しています。パターンだけでなくライン全体をどのように削除するか、ご存知ですか? "セクション3.1、"生物系のエネルギーとダイナミクス "、ページ337" - この行全体を削除... "mystring.pop(i)"を試しましたが、それは次のようになります:AttributeError: 'str' –

関連する問題