gpt4 book ai didi

python - 在 Python 中使用关键字打印句子

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:58 27 4
gpt4 key购买 nike

您好,我正在编写一个 Python 程序,它读取给定的 .txt 文件并查找关键字。在这个程序中,一旦我找到我的关键字(例如 'data'),我想打印出与该词相关联的整个句子。

我已经读入我的输入文件并使用 split() 方法去除空格、制表符和换行符并将所有单词放入一个数组中。

这是我到目前为止的代码。

text_file = open("file.txt", "r")
lines = []
lines = text_file.read().split()
keyword = 'data'

for token in lines:
if token == keyword:
//I have found my keyword, what methods can I use to
//print out the words before and after the keyword
//I have a feeling I want to use '.' as a marker for sentences
print(sentence) //prints the entire sentence

file.txt 内容如下

Welcome to SOF! This website securely stores data for the user.

期望的输出:

This website securely stores data for the user.

最佳答案

我们可以只在表示行尾的字符上拆分文本,然后遍历这些行并打印包含我们关键字的行。

要在多个字符上拆分文本,例如行尾可以用 标记! ? . 我们可以使用正则表达式:

import re

keyword = "data"
line_end_chars = "!", "?", "."
example = "Welcome to SOF! This website securely stores data for the user?"
regexPattern = '|'.join(map(re.escape, line_end_chars))
line_list = re.split(regexPattern, example)

# line_list looks like this:
# ['Welcome to SOF', ' This website securely stores data for the user', '']

# Now we just need to see which lines have our keyword
for line in line_list:
if keyword in line:
print(line)

But keep in mind that: if keyword in line: matches a sequence of characters, not necessarily a whole word - for example, 'data' in 'datamine' is True. If you only want to match whole words, you ought to use regular expressions: source explanation with example

Source for regex delimiters

关于python - 在 Python 中使用关键字打印句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com