Module RE, Have a couple questions

dasacc at gmail.com dasacc at gmail.com
Tue Mar 1 16:15:57 EST 2005


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]
[]
>>>




More information about the Python-list mailing list