(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 9 1201
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.co m 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('wor d') != -1 \
and line.find('word two'))] If there's another standard module more suited for this let me know, and no I dont want to use sed :)
Thanks
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.co m 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('wor d') != -1 \ and line.find('word two'))]
This should have been:
[line for line in document if (line.find('wor d') != -1 \
and line.find('word two') != -1)] If there's another standard module more suited for this let me know, and no I dont want to use sed :)
Thanks
Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit*: [line for line in document if (line.find('wor d') != -1 \ ********and line.find('word two') != -1)]
Hi,
Using re might be faster than scanning the same line twice :
=== begin snap
## rewords.py
import re
import sys
def iWordsMatch(lin es, word, word2):
reWordOneTwo = re.compile(r".* (%s|%s).*" % (word,word2))
return (line for line in lines if reWordOneTwo.ma tch(line))
for line in iWordsMatch(ope n("rewords.py") , "re", "return"):
sys.stdout.writ e(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
Francis Girard wrote: Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit :
[line for line in document if (line.find('wor d') != -1 \ and line.find('word two') != -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(lin es, word, word2): reWordOneTwo = re.compile(r".* (%s|%s).*" % (word,word2)) return (line for line in lines if reWordOneTwo.ma tch(line))
for line in iWordsMatch(ope n("rewords.py") , "re", "return"): sys.stdout.writ e(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
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(lin es, word, word2):
reWordOneTwo = re.compile(r".* ((%s.*%s)|(%s.* %s)).*" %
(word,word2,wor d2,word))
return (line for line in lines if reWordOneTwo.ma tch(line))
for line in iWordsMatch(ope n("rewords.py") , "re", "return"):
sys.stdout.writ e(line)
=== End SNAP
Regards,
Francis Girard
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('wor d') != -1 \ and line.find('word two') != -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.p y") to my object containing the data
for line in iWordsMatch(dat a, "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(ope n("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]
[]
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(lin es, word, word2):
reWordOneTwo = re.compile(r"(( %s.*%s)|(%s.*%s ))" %
(word,word2,wor d2,word))
return (line for line in lines if reWordOneTwo.se arch(line))
for line in iWordsMatch(ope n("rewords.py") , "re", "return"):
sys.stdout.writ e(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(lin es, word, word2): reWordOneTwo = re.compile(r".* ((%s.*%s)|(%s.* %s)).*" % (word,word2,wor d2,word)) return (line for line in lines if reWordOneTwo.ma tch(line))
for line in iWordsMatch(ope n("rewords.py") , "re", "return"): sys.stdout.writ e(line) === End SNAP
Regards,
Francis Girard
Hi,
Le mardi 1 Mars 2005 22:15, da****@gmail.co m 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('wor d') != -1 \ * * * * and line.find('word two') != -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(dat a, "microsoft" , "windows")
Same as above. Use:
for line in iWordsMatch(dat a.splitlines(), "microsoft" , "windows")
Why Microsoft and Windows ? I am very pleased to see that Microsoft Windowsis
now used instead of foo bar.
Regards
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 for my questions and
proposals?
|
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 = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE,
char*buf);
Users will get a Numeric Array object and can change its values (and
actually change the underlying C array).
|
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 regular expressions
easier to create and use (and in my experience as a regular expression
user, it makes them MUCH easier to create and use.)
I'm still working on formal documentation, and in any case, such
documentation isn't necessarily the...
|
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 a single
collections type, I should be proposing a new "namespaces" module
instead. Some of my reasons:
(1) Namespace is feeling less and less like a collection to me. Even
though it's still intended as a data-only structure, the use cases...
|
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 LOAD_GLOBAL 0 ( myDictionary )
3 LOAD_ATTR 1 ( has_key )
6 LOAD_CONST 1 ( ’e’ )
9 CALL_FUNCTION 1
12 PRINT_ITEM
13 PRINT_NEWLINE
| |
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 serialize
pass
|
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 main issue i am concerned with is performance....i would like to create a public object called Client, then i can set its properties from a Database, and have that object alive for the duration of the user's interaction with the system.....which helps...
|
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 installing Xcode I have
some sample scripts. They all start with
from CoreGraphics import *
|
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;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |