我需要打印包含一个特定单词或整个段落的两个句号之间的句子。如果采用以下线作为输入--超级磨料产品的需求与工业生产水平密切相关。超级磨具是用来制造持久的,昂贵的项目,如汽车和飞机零件,需求是高度周期性的。我想找到auto,它应该打印下一行。超级磨具是用来制造持久的,昂贵的项目,如汽车和飞机零件,需求是高度周期性的。
import os
from docx import Document
files = os.listdir()
contains=0
word=input("Enter the word to find :")
for file in files:
document = Document(file)
for paragraph in document.paragraphs:
if word in paragraph.text:
print("Filename :"+file)
print(" ")
print("Paragraphs" + paragraph.text)
print(" ")
contains=1
if(contains==0):
print("No result found in files")这是我写的,但它不能打印。
发布于 2022-06-01 15:18:16
s = 'The demand for Super Abrasive products is closely linked to the level of industrial production. Super Abrasives are used to manufacture long-lasting, expensive items like auto and aircraft parts, demand for which is highly cyclical.'
ls = s.split('.')
for i in ls:
if 'auto' in i:
print(i)上面的代码将打印“超级磨具是用来制造持久的,昂贵的项目,如汽车和飞机零件,其需求是高度周期性的”。
发布于 2022-06-01 15:19:42
给定一个类似于phrase的:
超级磨具是用来制造持久的,昂贵的项目,如汽车和飞机零件,需求是高度周期性的。
你可以试试这个:
keyword = "auto" # Define what substring you want to search
for sent in phrase.split("."):
if keyword in sent:
print(sent)将印刷:
超级磨具用于制造长效、昂贵的产品,如汽车和飞机零部件,其需求是高度周期性的
。
发布于 2022-06-01 15:20:49
您可以使用find命令:
for file in files:
document = Document(file)
for paragraph in document.paragraphs:
if paragraph.text.find(word) != -1 :
print("Filename :"+file)
print(" ")
print("Paragraphs" + paragraph.text)
print(" ")
contains=1https://stackoverflow.com/questions/72464235
复制相似问题