Re: [Tutor] Searching list items.

2006-10-17 Thread Luke Paireepinart
Chris Hengge wrote:
> Not sure if you were challenging me to think, or asking me, but I was 
> wanting to "line" to be printed... as in the string from the list. 
> What I got with re was what I'm assuming was a memory address.
What you got was an object.  If you try to print an object, all you get 
is the name of the object and the memory address of the object.
def aFunction():
print 'hi'

 >>> x = aFunction
 >>> print x


This is pretty useless, as Danny said.
what you want to do is actually use the object.
 >>> x()
hi

or without the intermediate variable:
 >>> aFunction()
hi

When you do a re search, you get a match object, which has methods such 
as...
well, Danny already gave you the site.
>
> match.  Take a look at a few of the methods in here:
>
>  http://www.python.org/doc/lib/match-objects.html
> 
>

Does this explanation help?

so if you just want to print the line, all you would do is check whether 
the re.search() call returned None or a match object.

As John Fouhy said:

for line in contents:
m = re.search('something', line)
if m:
print line[m.start():m.end()]

This example prints the portion of the line that matched the string you're 
searching for.

if you just want to print the line if it matches,
simply change the last line in the example to 'print line'.
note that the line
'if m'
could be rewritten
'if m != None:'

Or even (this may be Too Much Magic, so beware ;) )
'if m is not None'

For more advanced matching, you can actually use the match object that 
is returned.
but if you're just searching for a substring, then this will work.
Another good reason to use regular expressions is if you wanted to find, 
say,
the substring 'substring' with a 2-digit number on the end,
you'd have to do something like
substrs = ['substring'+str(a).zfill(2) for a in range(0,100)]

then
for item in substrs:
if item in line:
   print line

Whereas with a regular expression, you'd just do
regexp = re.compile('something\d\d')

then
if regexp.search(line):
print line

Computationally the last example should be much more efficient.
clearly it's also more complicated if you don't know regular expressions 
very well.
However, the substrs example is not too great either when it comes to 
readability.

HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Chris Hengge
Not sure if you were challenging me to think, or asking me, but I was wanting to "line" to be printed... as in the string from the list. What I got with re was what I'm assuming was a memory address. 
On 10/17/06, Danny Yoo <[EMAIL PROTECTED]> wrote:
> I remove those lines, but I was trying to use> for line in contents:>   result = re.search("something", line)>   print result'result' here is going to either be None, as you've seen, or a "match"
object.  We have several options available to use once we have amatch.  Take a look at a few of the methods in here: http://www.python.org/doc/lib/match-objects.html
Printing a match object itself won't be too informative.  What would youlike to show up if there is some match?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Danny Yoo


> I remove those lines, but I was trying to use
> for line in contents:
>   result = re.search("something", line)
>   print result

'result' here is going to either be None, as you've seen, or a "match" 
object.  We have several options available to use once we have a 
match.  Take a look at a few of the methods in here:

 http://www.python.org/doc/lib/match-objects.html

Printing a match object itself won't be too informative.  What would you 
like to show up if there is some match?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread John Fouhy
On 18/10/06, Chris Hengge <[EMAIL PROTECTED]> wrote:
> I remove those lines, but I was trying to use
> for line in contents:
> result = re.search("something", line)
> print result
>
> this printed out something like
>
> None
> None
> None
> hex memory address of goodness
> None
> None

If you don't need a regular expression, you are probably better off using 'in':

for line in contents:
if 'something' in line:
print line

Otherwise, re.search() and re.match() return either None or a match
object.  So, you can do this:

for line in contents:
m = re.search('something', line)
if m:
print line[m.start():m.end()]

match objects become more powerful when you start using groups or
named groups in your regular expressions.  See the documentation for
more :-)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Chris Hengge
An article at devshed was using it like that... but the output wasn't what I was looking for.. I was getting the hex address where the result was, but not printing the line.. I think the simple code you recommended in your first reply will do the trick, I'm not looking for anything magical, just need to see a line if it contains what I'm looking for.
I'll do some more checking into regex later, looks very useful...Thanks. On 10/17/06, Luke Paireepinart <
[EMAIL PROTECTED]> wrote:Chris Hengge wrote:> I remove those lines, but I was trying to use
> for line in contents:> result = re.search("something", line)> print resultI'm pretty sure this isn't how you use regular expressions.I have to go to class right now but if no one else has replied when I
get back I'll look into it.Sorry I can't help right now.-Luke>> this printed out something like>> None> None> None> hex memory address of goodness> None
> None
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Luke Paireepinart
Chris Hengge wrote:
> I remove those lines, but I was trying to use
> for line in contents:
> result = re.search("something", line)
> print result
I'm pretty sure this isn't how you use regular expressions.
I have to go to class right now but if no one else has replied when I 
get back I'll look into it.
Sorry I can't help right now.
-Luke
>
> this printed out something like
>
> None
> None
> None
> hex memory address of goodness
> None
> None

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Chris Hengge
I remove those lines, but I was trying to usefor line in contents:    result = re.search("something", line)    print resultthis printed out something like NoneNoneNonehex memory address of goodness
NoneNone...On 10/17/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
Chris Hengge wrote:> contents = readlines(myfile, 'r')> Ok, I'm under the impression this is a list of strings (array)Nope.  No such thing as arrays in Python.It is a list of strings, that's it. :)
> How in the world do I cycle through them looking for words?>> for line in contents:>  if line.contains("something")>  print line>> Thats a mock up of what I'm looking for.
> I tried to figure out how to use re, but that spits out locations in> memory or none it seems.You'd use regular expressions or useif 'astring' in line:if your use case is simple enough for that.
show us what RE you used that gave you trouble.It's hard to help when we don't know where you're getting stuck :)>> Thanks.Sure.-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching list items.

2006-10-17 Thread Luke Paireepinart
Chris Hengge wrote:
> contents = readlines(myfile, 'r')
> Ok, I'm under the impression this is a list of strings (array)
Nope.  No such thing as arrays in Python.
It is a list of strings, that's it. :)
> How in the world do I cycle through them looking for words?
>
> for line in contents:
>  if line.contains("something")
>  print line
>
> Thats a mock up of what I'm looking for.
> I tried to figure out how to use re, but that spits out locations in 
> memory or none it seems.
You'd use regular expressions or use
if 'astring' in line:

if your use case is simple enough for that.
show us what RE you used that gave you trouble.
It's hard to help when we don't know where you're getting stuck :)
>
> Thanks.
Sure.

-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Searching list items.

2006-10-17 Thread Chris Hengge
contents = readlines(myfile, 'r')Ok, I'm under the impression this is a list of strings (array)How in the world do I cycle through them looking for words?for line in contents: if line.contains("something")
 print lineThats a mock up of what I'm looking for. I tried to figure out how to use re, but that spits out locations in memory or none it seems. Thanks. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor