gpt4 book ai didi

python - 搜索文件中单词代码的输出错误

转载 作者:行者123 更新时间:2023-11-30 23:30:16 25 4
gpt4 key购买 nike

我有这段代码来搜索文件中的单词(而不是子字符串)并返回找到单词的行:

def word_search(word, file):
pattern = re.compile(r'\b{}\b'.format(re.escape(word), flags=re.IGNORECASE))
return (item for item in file
if pattern.match(item) == -1)

但是这段代码返回了(几乎)所有内容。我究竟做错了什么?感谢您的关注

这是代码:

sentences = re.split(r' *[\.\?!][\'"\)\]]* ]* *', text) # to split the file into sentences

def finding(word, file):
pattern = re.compile(r'\b{}\b'.format(re.escape(word)), flags=re.IGNORECASE)
return (item for item in file if pattern.search(item)) # your suggestion

from itertools import chain # I'm plannig on using more words, and I dont want duplicate #sentences. Thats why i use the chain + set.
chain = chain.from_iterable([finding('you', sentences), finding('us', sentences)])

plural_set = set(chain)

for sentence in plural_set:
outfile.write(sentence+'\r\n')

这给了我你在下面看到的结果。

这是测试文件的内容:

"Well, Mrs. Warren, I cannot see that you have any particular cause for uneasiness, nor do I understand why I, whose time is of some value, should interfere in the matter. I really have other things to engage me." So spoke Sherlock Holmes and turned back to the great scrapbook in which he was arranging and indexing some of his recent material.

But the landlady had the pertinacity and also the cunning of her sex. She held her ground firmly.

"Your arranged an affair for a lodger of mine last year," she said--"Mr. Fairdale Hobbs."

"Ah, yes--a simple matter."

"But he would never cease talking of it--your kindness, sir, and the way in which you brought light into the darkness. I remembered his words when I was in doubt and darkness myself. I know you could if you only would."

Holmes was accessible upon the side of flattery, and also, to do him justice, upon the side of kindliness. The two forces made him lay down his gum-brush with a sigh of resignation and push back his chair.

代码返回什么:

Warren, I cannot see that you have any particular cause for uneasiness, nor do I understand why I, whose time is of some value, should interfere in the matter So spoke Sherlock Holmes and turned back to the great scrapbook in which he was arranging and indexing some of his recent material.

But the landlady had the pertinacity and also the cunning of her sex. She held her ground firmly.

"Your arranged an affair for a lodger of mine last year," she said--"Mr. Fairdale Hobbs."

"Ah, yes--a simple matter."

"But he would never cease talking of it--your kindness, sir, and the way in which you brought light into the darkness I know you could if you only would."

Holmes was accessible upon the side of flattery, and also, to do him justice, upon the side of kindliness

最佳答案

存在三个错误:

  1. 当正则表达式无法匹配时,匹配函数返回 None,而不是 -1
  2. 如果您想匹配整个字符串而不是仅在某个字符串的开头,则需要使用 re.search() 而不是 re.match()字符串。
  3. 您需要在正确的位置提供 flags 参数:

所以它应该是这样的:

def word_search(word, file):
pattern = re.compile(r'\b{}\b'.format(re.escape(word)), flags=re.IGNORECASE)
return (item for item in file if pattern.search(item))

让我们看看它的实际效果:

>>> file = ["It's us or them.\n",
... '"Ah, yes--a simple matter."\n',
... 'Could you hold that for me?\n',
... 'Holmes was accessible upon the side of flattery, and also, to do him justice, upon the side of kindliness.\n',
... 'Trust your instincts.\n']
>>> list(word_search("us", file))
["It's us or them.\n"]
>>> list(word_search("you", file))
['Could you hold that for me?\n']

关于python - 搜索文件中单词代码的输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20724089/

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