Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87889 > unrolled thread

Regex Python Help

Started bygdotoli@gmail.com
First post2015-03-24 11:13 -0700
Last post2015-03-26 12:04 +0000
Articles 20 on this page of 23 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Regex Python Help gdotoli@gmail.com - 2015-03-24 11:13 -0700
    Re: Regex Python Help Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-24 13:23 -0500
    Re: Regex Python Help Gary Herron <gherron@digipen.edu> - 2015-03-24 11:25 -0700
      Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:10 -0700
        Re: Regex Python Help Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-03-24 19:40 +0000
    Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:22 -0700
      Re: Regex Python Help Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-03-24 20:40 +0100
      Re: Regex Python Help Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-24 14:35 -0500
      Re: Regex Python Help Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-03-24 20:38 +0100
      Re: Regex Python Help smap <askme.first@thankyouverymuch.invalid> - 2015-03-28 07:53 +0000
    Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:43 -0700
      Re: Regex Python Help Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-03-24 19:53 +0000
        Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 13:45 -0700
    Re: Regex Python Help Terry Reedy <tjreedy@udel.edu> - 2015-03-24 15:53 -0400
    Re: Regex Python Help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-25 12:43 +1100
      Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-25 07:53 -0700
    Re: Regex Python Help Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-25 20:34 +0000
      Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-25 14:19 -0700
        Re: Regex Python Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-25 22:15 +0000
        Re: Regex Python Help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-26 10:10 +1100
          Re: Regex Python Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-25 23:50 +0000
          Re: Regex Python Help Terry Reedy <tjreedy@udel.edu> - 2015-03-26 01:00 -0400
        Re: Regex Python Help Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-26 12:04 +0000

Page 1 of 2  [1] 2  Next page →


#87889 — Regex Python Help

Fromgdotoli@gmail.com
Date2015-03-24 11:13 -0700
SubjectRegex Python Help
Message-ID<099b0ca2-1f5e-4eb4-a7d0-d8210bcca51a@googlegroups.com>
I am creating a tool to search a filesystem for one simple string.
I cannot get the syntax correct.
Thank you in advance for your help.

import sys
import re
import os
path='/'
viewfiles=os.listdir(path)
for allfiles in viewfiles:
    file= os.path.join(path, allfiles)
text=open(file, "r")
for line in text:
    if re.match("DECRYPT_I", line):
        print line, 

----------------------------------------------------------
This should search every file for the simple regex "DECRYPT_I"
This is from the root down.

The error is:

SyntaxError: Missing parentheses in call to 'print'

Please help.
Gregg Dotoli

[toc] | [next] | [standalone]


#87890

FromSkip Montanaro <skip.montanaro@gmail.com>
Date2015-03-24 13:23 -0500
Message-ID<mailman.108.1427221435.10327.python-list@python.org>
In reply to#87889

[Multipart message — attachments visible in raw view] — view raw

On Tue, Mar 24, 2015 at 1:13 PM, <gdotoli@gmail.com> wrote:

> SyntaxError: Missing parentheses in call to 'print'


It appears you are attempting to use a Python 2.x print statement with
Python 3.x Try changing the last line to

print(line.rstrip())

Skip

[toc] | [prev] | [next] | [standalone]


#87891

FromGary Herron <gherron@digipen.edu>
Date2015-03-24 11:25 -0700
Message-ID<mailman.109.1427222047.10327.python-list@python.org>
In reply to#87889
On 03/24/2015 11:13 AM, gdotoli@gmail.com wrote:
> I am creating a tool to search a filesystem for one simple string.
> I cannot get the syntax correct.
> Thank you in advance for your help.
>
> import sys
> import re
> import os
> path='/'
> viewfiles=os.listdir(path)
> for allfiles in viewfiles:
>      file= os.path.join(path, allfiles)
> text=open(file, "r")
> for line in text:
>      if re.match("DECRYPT_I", line):
>          print line,
>
> ----------------------------------------------------------
> This should search every file for the simple regex "DECRYPT_I"
> This is from the root down.
>
> The error is:
>
> SyntaxError: Missing parentheses in call to 'print'
>
> Please help.
> Gregg Dotoli

You appear to be using Python3, but have written code for Python2.

There are a number of differences between the two, but your particular 
error runs into the different syntax for prints.

Python2: print line # This is a statement
Python3  print(line)  # This is a procedure call



-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

[toc] | [prev] | [next] | [standalone]


#87893

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-24 12:10 -0700
Message-ID<ba78e279-4bdc-4d1a-8135-f56e5dd22b09@googlegroups.com>
In reply to#87891
Thank you Gary, that got rid of the error, but now there is no tree walk, it runs and immediatley finishes. I just need to grep each file. I have this working with the windows "for /r %a and redirecting that to Python, but want to use Python only. I do have dummy files with the regex string.

Thanks again,
Gregg




On Tuesday, March 24, 2015 at 2:34:20 PM UTC-4, Gary Herron wrote:
> On 03/24/2015 11:13 AM, gdotoli@gmail.com wrote:
> > I am creating a tool to search a filesystem for one simple string.
> > I cannot get the syntax correct.
> > Thank you in advance for your help.
> >
> > import sys
> > import re
> > import os
> > path='/'
> > viewfiles=os.listdir(path)
> > for allfiles in viewfiles:
> >      file= os.path.join(path, allfiles)
> > text=open(file, "r")
> > for line in text:
> >      if re.match("DECRYPT_I", line):
> >          print line,
> >
> > ----------------------------------------------------------
> > This should search every file for the simple regex "DECRYPT_I"
> > This is from the root down.
> >
> > The error is:
> >
> > SyntaxError: Missing parentheses in call to 'print'
> >
> > Please help.
> > Gregg Dotoli
> 
> You appear to be using Python3, but have written code for Python2.
> 
> There are a number of differences between the two, but your particular 
> error runs into the different syntax for prints.
> 
> Python2: print line # This is a statement
> Python3  print(line)  # This is a procedure call
> 
> 
> 
> -- 
> Dr. Gary Herron
> Department of Computer Science
> DigiPen Institute of Technology
> (425) 895-4418

[toc] | [prev] | [next] | [standalone]


#87897

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-03-24 19:40 +0000
Message-ID<meseik$amn$3@dont-email.me>
In reply to#87893
On Tue, 24 Mar 2015 12:10:24 -0700, Gregg Dotoli wrote:
> 
> Thank you Gary, that got rid of the error, but now there is no tree
> walk, it runs and immediatley finishes. I just need to grep each file. I
> have this working with the windows "for /r %a and redirecting that to
> Python, but want to use Python only. I do have dummy files with the
> regex string.
> 
> Thanks again,
> Gregg
> 

Gregg --

First, please don't top-post.  Secondly, os.listdir only does exactly 
that; it lists everything on one directory.  You're looking for os.walk.

-- Rob

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#87894

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-24 12:22 -0700
Message-ID<43835701-a845-4a59-8936-0be236dfa59f@googlegroups.com>
In reply to#87889
Thank you! But

The print error is gone, but now the script quickly finishes and doesnt walk
the OS tree or search.

Gregg 



On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote:
> I am creating a tool to search a filesystem for one simple string.
> I cannot get the syntax correct.
> Thank you in advance for your help.
> 
> import sys
> import re
> import os
> path='/'
> viewfiles=os.listdir(path)
> for allfiles in viewfiles:
>     file= os.path.join(path, allfiles)
> text=open(file, "r")
> for line in text:
>     if re.match("DECRYPT_I", line):
>         print line, 
> 
> ----------------------------------------------------------
> This should search every file for the simple regex "DECRYPT_I"
> This is from the root down.
> 
> The error is:
> 
> SyntaxError: Missing parentheses in call to 'print'
> 
> Please help.
> Gregg Dotoli

[toc] | [prev] | [next] | [standalone]


#87896

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2015-03-24 20:40 +0100
Message-ID<mailman.111.1427226044.10327.python-list@python.org>
In reply to#87894
Le 24/03/2015 20:38, Vincent Vande Vyvre a écrit :
> Le 24/03/2015 20:22, Gregg Dotoli a écrit :
>> Thank you! But
>>
>> The print error is gone, but now the script quickly finishes and 
>> doesnt walk
>> the OS tree or search.
>>
>> Gregg
>>
>>
>>
>> On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote:
>>> I am creating a tool to search a filesystem for one simple string.
>>> I cannot get the syntax correct.
>>> Thank you in advance for your help.
>>>
>>> import sys
>>> import re
>>> import os
>>> path='/'
>>> viewfiles=os.listdir(path)
>>> for allfiles in viewfiles:
>>>      file= os.path.join(path, allfiles)
>>> text=open(file, "r")
>>> for line in text:
>>>      if re.match("DECRYPT_I", line):
>>>          print line,
>>>
>>> ----------------------------------------------------------
>>> This should search every file for the simple regex "DECRYPT_I"
>>> This is from the root down.
>>>
>>> The error is:
>>>
>>> SyntaxError: Missing parentheses in call to 'print'
>>>
>>> Please help.
>>> Gregg Dotoli
> Your indentation is wrong.
>
> for allfiles in viewfiles:
>     file= os.path.join(path, allfiles)
>     text=open(file, "r")
>     for line in text:
>     if re.match("DECRYPT_I", line):
>         print line,
>
>
> Vincent
... and me too

for allfiles in viewfiles:
     file= os.path.join(path, allfiles)
     text=open(file, "r")
     for line in text:
         if re.match("DECRYPT_I", line):
             print line,

[toc] | [prev] | [next] | [standalone]


#87898

FromSkip Montanaro <skip.montanaro@gmail.com>
Date2015-03-24 14:35 -0500
Message-ID<mailman.112.1427226114.10327.python-list@python.org>
In reply to#87894

[Multipart message — attachments visible in raw view] — view raw

On Tue, Mar 24, 2015 at 2:22 PM, Gregg Dotoli <gdotoli@gmail.com> wrote:

> The print error is gone, but now the script quickly finishes and doesnt
> walk
> the OS tree or search.
>

You need to walk the directory tree recursively. Take a look at os.walk().

Skip

[toc] | [prev] | [next] | [standalone]


#87900

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2015-03-24 20:38 +0100
Message-ID<mailman.113.1427226623.10327.python-list@python.org>
In reply to#87894
Le 24/03/2015 20:22, Gregg Dotoli a écrit :
> Thank you! But
>
> The print error is gone, but now the script quickly finishes and doesnt walk
> the OS tree or search.
>
> Gregg
>
>
>
> On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote:
>> I am creating a tool to search a filesystem for one simple string.
>> I cannot get the syntax correct.
>> Thank you in advance for your help.
>>
>> import sys
>> import re
>> import os
>> path='/'
>> viewfiles=os.listdir(path)
>> for allfiles in viewfiles:
>>      file= os.path.join(path, allfiles)
>> text=open(file, "r")
>> for line in text:
>>      if re.match("DECRYPT_I", line):
>>          print line,
>>
>> ----------------------------------------------------------
>> This should search every file for the simple regex "DECRYPT_I"
>> This is from the root down.
>>
>> The error is:
>>
>> SyntaxError: Missing parentheses in call to 'print'
>>
>> Please help.
>> Gregg Dotoli
Your indentation is wrong.

for allfiles in viewfiles:
     file= os.path.join(path, allfiles)
     text=open(file, "r")
     for line in text:
     if re.match("DECRYPT_I", line):
         print line,


Vincent

[toc] | [prev] | [next] | [standalone]


#88210

Fromsmap <askme.first@thankyouverymuch.invalid>
Date2015-03-28 07:53 +0000
Message-ID<Y5tRw.454854$mB1.182625@fx32.am4>
In reply to#87894
On Tue, 24 Mar 2015 12:22:38 -0700, Gregg Dotoli wrote:

> Thank you! But
> 
> The print error is gone, but now the script quickly finishes and doesnt
> walk the OS tree or search.
> 
> Gregg
> 
> 
> 
> On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote:
>> I am creating a tool to search a filesystem for one simple string.

bloody top poster

plonk

[toc] | [prev] | [next] | [standalone]


#87899

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-24 12:43 -0700
Message-ID<06bb23d6-9fc7-4f4e-a57d-161a0a496597@googlegroups.com>
In reply to#87889
This works fine , but I have to pipe the filename to the script python stool <foo

All I need is a loop, should I bag Python and use a simple shell for loop?


import sys
import re
import os
pattern="DECRYPT_I"
regexp=re.compile(pattern)
for line in sys.stdin:
    result=regexp.search(line)
    if result:
        sys.stdout.write(line)


































On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote:
> I am creating a tool to search a filesystem for one simple string.
> I cannot get the syntax correct.
> Thank you in advance for your help.
> 
> import sys
> import re
> import os
> path='/'
> viewfiles=os.listdir(path)
> for allfiles in viewfiles:
>     file= os.path.join(path, allfiles)
> text=open(file, "r")
> for line in text:
>     if re.match("DECRYPT_I", line):
>         print line, 
> 
> ----------------------------------------------------------
> This should search every file for the simple regex "DECRYPT_I"
> This is from the root down.
> 
> The error is:
> 
> SyntaxError: Missing parentheses in call to 'print'
> 
> Please help.
> Gregg Dotoli

[toc] | [prev] | [next] | [standalone]


#87904

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-03-24 19:53 +0000
Message-ID<mesfbk$amn$4@dont-email.me>
In reply to#87899
On Tue, 24 Mar 2015 12:43:38 -0700, Gregg Dotoli wrote:

> [context snipped due to top posting]
>
> All I need is a loop, should I bag Python and use a simple shell for 
loop?

Honestly, yes.  You're not even using a regular expression, just a fixed 
string you're trying to search for.  You can do the entire thing as

$ find . -type f | xargs grep DECRYPT_I

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#87908

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-24 13:45 -0700
Message-ID<cc35fced-c7af-4b74-988c-eb0bab815344@googlegroups.com>
In reply to#87904
Here you go. Windows shell was easier!!!

for /f %a in (c:\gonow) do echo %a | c:\Python34\python c:\python34\unopy.py %a

Now I can use any regex pattern, I need.


On Tuesday, March 24, 2015 at 3:54:25 PM UTC-4, Rob Gaddi wrote:
> On Tue, 24 Mar 2015 12:43:38 -0700, Gregg Dotoli wrote:
> 
> > [context snipped due to top posting]
> >
> > All I need is a loop, should I bag Python and use a simple shell for 
> loop?
> 
> Honestly, yes.  You're not even using a regular expression, just a fixed 
> string you're trying to search for.  You can do the entire thing as
> 
> $ find . -type f | xargs grep DECRYPT_I
> 
> -- 
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com
> Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#87903

FromTerry Reedy <tjreedy@udel.edu>
Date2015-03-24 15:53 -0400
Message-ID<mailman.115.1427226832.10327.python-list@python.org>
In reply to#87889
On 3/24/2015 2:13 PM, gdotoli@gmail.com wrote:
> I am creating a tool to search a filesystem for one simple string.
> I cannot get the syntax correct.
> Thank you in advance for your help.
>
> import sys
> import re
> import os
> path='/'
> viewfiles=os.listdir(path)

listdir is not recursive, so this code will only search files in the one 
directory, not the whole filesystem.  You need to use os.walk and modify 
the code to do the latter.

> for allfiles in viewfiles:
>      file= os.path.join(path, allfiles)
> text=open(file, "r")
> for line in text:
>      if re.match("DECRYPT_I", line):
>          print line,

You appear to have used a mixture of spaces and tabs for indents.  That 
works in 2.x, but not in 3.x.  You open but do not close files, which 
could be a problem if you open and search 100000 files in a filesystem. 
  Use a with statememt.  'allfiles' is a bad name because it get bound 
to a single file.

for file in viewfiles:
     with open(os.path.join(path, file)) as text:
         for line in text:
             if re.match("DECRYPT_I", line):
                 print(line)

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#87917

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-03-25 12:43 +1100
Message-ID<551212d1$0$13006$c3e8da3$5496439d@news.astraweb.com>
In reply to#87889
On Wed, 25 Mar 2015 05:13 am, gdotoli@gmail.com wrote:

> The error is:
> 
> SyntaxError: Missing parentheses in call to 'print'


I cannot imagine how the message could be more explicit: the call to print
is missing parentheses. If you're not going to read the error messages you
are given, you are truly going to struggle as a programmer.

Read the error message.  If you don't understand the error message, please
say so.

And choose a relevant subject line: this has nothing to do with regexes. You
might as well have called it "Import Python Help".


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#87949

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-25 07:53 -0700
Message-ID<59bd09bc-83f8-4b62-9288-07ba6f901d3d@googlegroups.com>
In reply to#87917
No worries Steven, 
Thanks to ALL on this thread.

Gregg



On Tuesday, March 24, 2015 at 9:43:58 PM UTC-4, Steven D'Aprano wrote:
> On Wed, 25 Mar 2015 05:13 am, gdotoli@gmail.com wrote:
> 
> > The error is:
> > 
> > SyntaxError: Missing parentheses in call to 'print'
> 
> 
> I cannot imagine how the message could be more explicit: the call to print
> is missing parentheses. If you're not going to read the error messages you
> are given, you are truly going to struggle as a programmer.
> 
> Read the error message.  If you don't understand the error message, please
> say so.
> 
> And choose a relevant subject line: this has nothing to do with regexes. You
> might as well have called it "Import Python Help".
> 
> 
> -- 
> Steven

[toc] | [prev] | [next] | [standalone]


#87980

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-03-25 20:34 +0000
Message-ID<mev65g$903$2@dont-email.me>
In reply to#87889
On Tue, 24 Mar 2015 11:13:41 -0700, gdotoli wrote:

> I am creating a tool to search a filesystem for one simple string.

man grep

STOP! REINVENTING! THE! WHEEL!

Your new wheel will invariably be slower and less efficient than the old 
one.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#87984

FromGregg Dotoli <gdotoli@gmail.com>
Date2015-03-25 14:19 -0700
Message-ID<94a16553-1d08-46ca-9290-d024cf408248@googlegroups.com>
In reply to#87980
Grep is regular expressions. If I'm using Python, I'll use the Python modules.
Silly

Gregg

On Wednesday, March 25, 2015 at 4:36:01 PM UTC-4, Denis McMahon wrote:
> On Tue, 24 Mar 2015 11:13:41 -0700, gdotoli wrote:
> 
> > I am creating a tool to search a filesystem for one simple string.
> 
> man grep
> 
> STOP! REINVENTING! THE! WHEEL!
> 
> Your new wheel will invariably be slower and less efficient than the old 
> one.
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#87986

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-03-25 22:15 +0000
Message-ID<mailman.163.1427321756.10327.python-list@python.org>
In reply to#87984
On 25/03/2015 21:19, Gregg Dotoli wrote:
> Grep is regular expressions. If I'm using Python, I'll use the Python modules.
> Silly
>
> Gregg
>

Clear as mud, and please don't top post.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#87991

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-03-26 10:10 +1100
Message-ID<5513404c$0$13000$c3e8da3$5496439d@news.astraweb.com>
In reply to#87984
On Thu, 26 Mar 2015 08:19 am, Gregg Dotoli wrote:

> Grep is regular expressions. If I'm using Python, I'll use the Python
> modules. Silly

It very well may be silly.

You're using Python regular expressions because you're using Python. Why are
you using Python? Early in this thread you said 


"Here you go. Windows shell was easier!!!"


Does Windows shell have grep? How about Powershell? If grep is available,
why mix two different languages?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web