如何使用 python 匹配文本文件中的单词?
我想搜索并匹配文本文件中的特定单词。
with open('wordlist.txt', 'r') as searchfile:
for line in searchfile:
if word in line:
print line
此代码甚至返回包含目标单词子字符串的单词。例如,如果单词是“there”,则搜索将返回“there”、“therefore”、“thereby”等。
我希望代码仅返回包含“there”的行。时期。
I want to search and match a particular word in a text file.
with open('wordlist.txt', 'r') as searchfile:
for line in searchfile:
if word in line:
print line
This code returns even the words that contain substrings of the target word. For example if the word is "there" then the search returns "there", "therefore", "thereby", etc.
I want the code to return only the lines which contain "there". Period.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
re.search
函数扫描字符串line
,如果找到第一个参数中定义的正则表达式,则返回 true,忽略re.I
。^
字符表示“行的开头”,而$
字符表示“行的结尾”。因此,搜索函数只有在匹配前面是行首、后面是行尾的there时才会返回 true,也称为独立的。The
re.search
function scans the stringline
and returns true if it finds the regular expression defined in the first parameter, ignoring case withre.I
. The^
character means 'beginning of the line' while the$
character means 'end of the line'. Therefore, the search function will only return true if it matches there preceded by the beginning of the line, and followed by the end of the line, aka isolated on its own.将行拆分为标记:
if word in line.split():
split the line into tokens:
if word in line.split():
您始终可以使用正则表达式,类似于:
\sthere\s
- 任何空格后跟“there”,后跟任何空格re.I
- 表示不区分大小写You can always use regex, something along the lines of:
\sthere\s
- any space followed by 'there' followed by any spacere.I
- means case insensitivere.M
- doesn't really matter in this case (since lines only have 1 \n)你应该使用正则表达式。 Python 文档中的正则表达式指南可能是一个不错的起点。
You ought to use a regular expression. The regular expression howto from the Python docs might be a good place to start.
查找 re 模块(正则表达式)。 re.search 使用正则表达式 'there' 就是你想要的。
Look up the re module (regular expressions). re.search with the regex ' there ' is what you want.