473,513 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Module RE, Have a couple questions

(1) How do I perform a search for "word" and have it return every line
that this instance is found?

(2) How do I perform a search for "word" and "wordtwo" at the same time
to return every line these instances are found so that the order in
which these lines are in are left intact.

If there's another standard module more suited for this let me know,
and no I dont want to use sed :)

Thanks

Jul 18 '05 #1
9 1189
Dasacc

There is a better (faster/easier) way to do it than using the re module,
the find method of the string class.

da****@gmail.com wrote:
(1) How do I perform a search for "word" and have it return every line
that this instance is found?
[line for line in document if line.find('a') != -1]

(2) How do I perform a search for "word" and "wordtwo" at the same time
to return every line these instances are found so that the order in
which these lines are in are left intact.
[line for line in document if (line.find('word') != -1 \
and line.find('wordtwo'))]

If there's another standard module more suited for this let me know,
and no I dont want to use sed :)

Thanks

Jul 18 '05 #2
Oops, made a mistake.

Marc Huffnagle wrote:
Dasacc

There is a better (faster/easier) way to do it than using the re module,
the find method of the string class.

da****@gmail.com wrote:
(1) How do I perform a search for "word" and have it return every line
that this instance is found?

[line for line in document if line.find('a') != -1]

(2) How do I perform a search for "word" and "wordtwo" at the same time
to return every line these instances are found so that the order in
which these lines are in are left intact.

[line for line in document if (line.find('word') != -1 \
and line.find('wordtwo'))]


This should have been:

[line for line in document if (line.find('word') != -1 \
and line.find('wordtwo') != -1)]

If there's another standard module more suited for this let me know,
and no I dont want to use sed :)

Thanks

Jul 18 '05 #3
Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit*:
[line for line in document if (line.find('word') != -1 \
********and line.find('wordtwo') != -1)]


Hi,

Using re might be faster than scanning the same line twice :

=== begin snap
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
reWordOneTwo = re.compile(r".*(%s|%s).*" % (word,word2))
return (line for line in lines if reWordOneTwo.match(line))

for line in iWordsMatch(open("rewords.py"), "re", "return"):
sys.stdout.write(line)
=== end snap

Furthermore, using list comprehension generator (2.4 only I think) and file
iterator, you can scan files as big as you want with very little memory
usage.

Regards,

Francis Girard

Jul 18 '05 #4
Francis Girard wrote:
Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit :
[line for line in document if (line.find('word') != -1 \
and line.find('wordtwo') != -1)]

Hi,

Using re might be faster than scanning the same line twice :


My understanding of the second question was that he wanted to find lines
which contained both words but, looking at it again, it could go either
way. If he wants to find lines that contain both of the words, in any
order, then I don't think that it can be done without scanning the line
twice (regex or not).

To the OP: What kind of data are you testing? Could you try both of
these solutions on your sample data and let us know which runs faster?

=== begin snap
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
reWordOneTwo = re.compile(r".*(%s|%s).*" % (word,word2))
return (line for line in lines if reWordOneTwo.match(line))

for line in iWordsMatch(open("rewords.py"), "re", "return"):
sys.stdout.write(line)
=== end snap

Furthermore, using list comprehension generator (2.4 only I think) and file
iterator, you can scan files as big as you want with very little memory
usage.

Regards,

Francis Girard

Jul 18 '05 #5
Le mardi 1 Mars 2005 21:38, Marc Huffnagle a écrit*:
My understanding of the second question was that he wanted to find lines
which contained both words but, looking at it again, it could go either
way. *If he wants to find lines that contain both of the words, in any
order, then I don't think that it can be done without scanning the line
twice (regex or not).


I don't know if it is really faster but here's a version that finds both words
on the same line. My understanding is that re needs to parse the line only
once. This might count on very large inputs.

=== Begin SNAP
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
reWordOneTwo = re.compile(r".*((%s.*%s)|(%s.*%s)).*" %
(word,word2,word2,word))
return (line for line in lines if reWordOneTwo.match(line))

for line in iWordsMatch(open("rewords.py"), "re", "return"):
sys.stdout.write(line)
=== End SNAP

Regards,

Francis Girard



Jul 18 '05 #6
I'm still very new to python (my 2nd day atm) but this is what I come
up with.

First note, I wasn't clear (I reread what I wrote) about my 'word'
'wordtwo' problem. Both words do Not need to be on the same line. But
rather say there was

Line 4: This is a line
Line 5: Yet another one
Line 6: its a line

I'd like to perform a search for 'line' and 'one' and get the above
returned in the same order. They don't both necessarily need to be
together on the same line.

Now I don't know this stuff very well but I dont think the code
[line for line in document if (line.find('word') != -1 \
and line.find('wordtwo') != -1)]

would do this as it answers the question in how you thought I asked.

Also I can't seem to implement your code to work with more than a
single letter. For example if I do this from the python shell

var1 = "this is a test\nand another test"
[line for line in var1 if line.find('t') != -1]

then I get 6 results of 't' which is correct, But if i try

var1 = "this is a test\nand another test"
[line for line in var1 if line.find('test') != -1]

I get no returned results for test. I tried doing this after I couldn't
retrieve any data larger than a single letter from my source file.

As per Francis Girard's suggestion, it seems to work as long as the
data is in a file.

if I change open("rewords.py") to my object containing the data

for line in iWordsMatch(data, "microsoft", "windows"):

I get no results. But if I instead first write this data to file

open('output', 'w').write(data)

and then run the suggestions as

for line in iWordsMatch(open("output"), "re", "return"):

it works fine for all practical purposes.

I'd still like to know how to use your suggestion beyond a single
character. Am I goofing something up with: ?
var1 = "this is a test\nand another test"
[line for line in var1 if line.find('test') != -1] []


Jul 18 '05 #7
Hi,

This might even be faster since using re.search, we don't need to parse the
whole line.

Regards,

Francis Girard
=== BEGIN SNAP
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
reWordOneTwo = re.compile(r"((%s.*%s)|(%s.*%s))" %
(word,word2,word2,word))
return (line for line in lines if reWordOneTwo.search(line))

for line in iWordsMatch(open("rewords.py"), "re", "return"):
sys.stdout.write(line)
=== END SNAP


Le mardi 1 Mars 2005 21:57, Francis Girard a écrit*:
Le mardi 1 Mars 2005 21:38, Marc Huffnagle a écrit*:
My understanding of the second question was that he wanted to find lines
which contained both words but, looking at it again, it could go either
way. *If he wants to find lines that contain both of the words, in any
order, then I don't think that it can be done without scanning the line
twice (regex or not).


I don't know if it is really faster but here's a version that finds both
words on the same line. My understanding is that re needs to parse the line
only once. This might count on very large inputs.

=== Begin SNAP
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
reWordOneTwo = re.compile(r".*((%s.*%s)|(%s.*%s)).*" %
(word,word2,word2,word))
return (line for line in lines if reWordOneTwo.match(line))

for line in iWordsMatch(open("rewords.py"), "re", "return"):
sys.stdout.write(line)
=== End SNAP

Regards,

Francis Girard


Jul 18 '05 #8
Hi,

Le mardi 1 Mars 2005 22:15, da****@gmail.com a écrit*:
Now I don't know this stuff very well but I dont think the code
[line for line in document if (line.find('word') != -1 \
* * * * and line.find('wordtwo') != -1)]
would do this as it answers the question in how you thought I asked.


Just use "or" instead of "and" and you'll get what you need.
var1 = "this is a test\nand another test"
[line for line in var1 if line.find('t') != -1]
You are scanning the letters in the string.
You first need to split your input into lines, in order to scan over strings
in a list of strings. Use :

var1 = "this is a test\nand another test"
[line for line in var1.splitlines() if line.find('t') != -1]
for line in iWordsMatch(data, "microsoft", "windows")


Same as above. Use:

for line in iWordsMatch(data.splitlines(), "microsoft", "windows")

Why Microsoft and Windows ? I am very pleased to see that Microsoft Windowsis
now used instead of foo bar.

Regards

Jul 18 '05 #9
Why Microsoft and Windows ?

B/c it was actually in the data I was trying to parse (though not
something I was needing to parse), I obscured everything except my test
search terms *shrugs*

I saw something on this group about 'to many "or's"' so I figured it
was an option. Thanks for the .splitlines() pointer, it fixed my woe's
I was experiencing as stated earlier.

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1456
by: Åâãåíèé Êîñåíêî | last post by:
Hi! I have several questions about the module datetime. The module has been added to Python 2.3. It seems that its interface is raw and dirty. Who is maintainer of the module? Who may answer...
8
3838
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
1
4155
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
18
3012
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
0
2580
by: zhu_dave | last post by:
Hello, I have a couple of questions on the dis module. Let's say that I have the following disassembled code (It's a simple program which checks to see whether key 'e' is in myDictionary): 0...
30
2035
by: Franck PEREZ | last post by:
Hello, I'm developing a small XML marshaller and I'm facing an annoying issue. Here's some sample code: ########### My test application ############ class Foo(object): #The class I'd like to...
4
2153
by: semsem22 | last post by:
hello, i m new to ASP.NET, but have experience with VB6....i have a couple of questions about the use of a module and the decleration of public objects in the module vs the use of sessions the...
10
1904
by: David C. Ullrich | last post by:
Running OS X 10.4 "Tiger". Various references by Apple and others say that there exists a module that gives Quartz bindings, allowing all sort of graphics things in Python. Sure enough, after...
8
1579
by: Derek Martin | last post by:
I'd like to know if it's possible to code something in Python which would be equivalent to the following C: ---- debug.c ---- #include <stdio.h> bool DEBUG;
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.