テキストファイルに小説を貼り付けました。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:文字列またはその他の文字バッファオブジェクト
これは大変感謝しています。パターンだけでなくライン全体をどのように削除するか、ご存知ですか? "セクション3.1、"生物系のエネルギーとダイナミクス "、ページ337" - この行全体を削除... "mystring.pop(i)"を試しましたが、それは次のようになります:AttributeError: 'str' –