匹配文件行中的2个单词 [英] Match 2 words in a line of file
问题描述
嗨
我对python很新,因此这个问题..
我有一个进程输出的文件。我需要一次搜索这个文件一行
行,我的模式是我正在寻找
有事件这个词的行和这个词''new''。
请注意,我需要的行只有两个单词,而不是其中一行
...
我如何为此写一个正则表达式..或者更好,我甚至使用
regexp或者有更好的方法来做到这一点....
谢谢
Hi
Am pretty new to python and hence this question..
I have file with an output of a process. I need to search this file one
line at a time and my pattern is that I am looking for the lines that
has the word ''event'' and the word ''new''.
Note that i need lines that has both the words only and not either one
of them..
how do i write a regexp for this.. or better yet shd i even be using
regexp or is there a better way to do this....
thanks
推荐答案
el ********* @ gmail.com 写道:
嗨
我对python很新,因此这个问题..
我有一个进程输出的文件。我需要一次搜索这个文件一行
行,我的模式是我正在寻找
有事件这个词的行和这个词''new''。
请注意,我需要的行只有两个单词,而不是其中一行
...
我如何为此写一个正则表达式..或者更好,我甚至使用
regexp或者有更好的方法来做到这一点....
谢谢
Hi
Am pretty new to python and hence this question..
I have file with an output of a process. I need to search this file one
line at a time and my pattern is that I am looking for the lines that
has the word ''event'' and the word ''new''.
Note that i need lines that has both the words only and not either one
of them..
how do i write a regexp for this.. or better yet shd i even be using
regexp or is there a better way to do this....
thanks
也许这样的事情可以做到:
import re
def lines_with_words(file,word1,word2):
"""打印文件中包含两个单词的所有行。"" ;
for line in file:
if re.search(r" \b" + word1 + r" \b",line)and \
re.search(r" \b" + word2 + r" \b",line):
打印行
只需使用文件对象调用该函数即可两个字符串
代表你想在每一行中找到的单词。
要匹配正则表达式中的单词,你要写\ bWORD \\ \\ b" ;.
我不知道是否有更好的方法可以做到这一点,但我相信
这至少应该起作用。
Maybe something like this would do:
import re
def lines_with_words(file, word1, word2):
"""Print all lines in file that have both words in it."""
for line in file:
if re.search(r"\b" + word1 + r"\b", line) and \
re.search(r"\b" + word2 + r"\b", line):
print line
Just call the function with a file object and two strings that
represent the words that you want to find in each line.
To match a word in regex you write "\bWORD\b".
I don''t know if there is a better way of doing this, but I believe that
this should at least work.
不使用re,这可能有效(未经测试;-):
def lines_with_words(file,word1 ,word2):
"""打印文件中包含两个单词的所有行。""
for line in file:
words = line.split()
如果单词中的word1和单词中的word2:
打印行
/ Jean Brouwers
Rickard Lindberg写道:
Without using re, this may work (untested ;-):
def lines_with_words(file, word1, word2):
"""Print all lines in file that have both words in it."""
for line in file:
words = line.split()
if word1 in words and word2 in words:
print line
/Jean Brouwers
Rickard Lindberg wrote:
el *********@gmail.com 写道:
嗨
很漂亮新的python因此这个问题..
我有文件与进程的输出。我需要一次搜索这个文件一行
行,我的模式是我正在寻找
有事件这个词的行和这个词''new''。
请注意,我需要的行只有两个单词,而不是其中一行
...
我如何为此写一个正则表达式..或者更好,我甚至使用
regexp或者有更好的方法来做到这一点....
谢谢
Hi
Am pretty new to python and hence this question..
I have file with an output of a process. I need to search this file one
line at a time and my pattern is that I am looking for the lines that
has the word ''event'' and the word ''new''.
Note that i need lines that has both the words only and not either one
of them..
how do i write a regexp for this.. or better yet shd i even be using
regexp or is there a better way to do this....
thanks
也许这样的事情可以做到:
import re >
def lines_with_words(file,word1,word2):
"""打印文件中包含两个单词的所有行。"" ;
for line in file:
if re.search(r" \b" + word1 + r" \b",line)and \
re.search(r" \b" + word2 + r" \b",line):
打印行
只需用一个文件对象和两个字符串调用该函数,
代表你想在每一行中找到的单词。
要匹配你写的正则表达式中的一个单词\ bWORD \b。
我不知道是否有更好的方法可以做到这一点,但我相信
这应该至少有效。
Maybe something like this would do:
import re
def lines_with_words(file, word1, word2):
"""Print all lines in file that have both words in it."""
for line in file:
if re.search(r"\b" + word1 + r"\b", line) and \
re.search(r"\b" + word2 + r"\b", line):
print line
Just call the function with a file object and two strings that
represent the words that you want to find in each line.
To match a word in regex you write "\bWORD\b".
I don''t know if there is a better way of doing this, but I believe that
this should at least work.
MrJean1写道:
MrJean1 wrote:
def lines_with_words(file,word1,word2):
"""打印文件中包含两个单词的所有行。""
for line in file:
words = line.split()
如果单词中的word1和单词中的word2:
打印行
def lines_with_words(file, word1, word2):
"""Print all lines in file that have both words in it."""
for line in file:
words = line.split()
if word1 in words and word2 in words:
print line
这听起来更好,它可能比RE版本更快,Python
2.5有一个非常快的str .__ contains__方法,由effbot完成:
def lines_with_words(file,word1,word2):
"""打印文件中包含两个单词的所有行。
(word1可能是这两个词的频率较低。)&;
for line in file:
如果word1在行,word2在行:
打印线
再见,
。熊市
This sounds better, it''s probably faster than the RE version, Python
2.5 has a really fast str.__contains__ method, done by effbot:
def lines_with_words(file, word1, word2):
"""Print all lines in file that have both words in it.
(word1 may be the less frequent word of the two)."""
for line in file:
if word1 in line and word2 in line:
print line
Bye,
bearophile
这篇关于匹配文件行中的2个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!