Я вставил роман в текстовый файл. Я хотел бы, чтобы удалить все строки, содержащие в следующих предложениях, как они держат происходящие в верхней части каждой страницы (только удаление их вхождения в этих строках будут делать, а):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,« Энергетика и динамика биологических систем »,« удалить всю эту строку ... »Я попытался« mystring.pop (i) », но он дает: AttributeError: объект« str »не имеет атрибута« pop », –