dudes dudes | 2 Oct 10:36 2011
Picon

matching strings in python

Hello,

Can someone kindly point me my mistake here in this code. I'm trying to search a file for 10000 OR 10001 (in python).

import re;

this= open("file", r)
for line in this:
   if re.match('(10000)' | '(10001)', line)
        print line

/*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */


thanks for your time
ak

--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Tethys | 2 Oct 10:45 2011
X-Face
Picon

Re: matching strings in python


dudes dudes writes:

>/*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */

I might be more inclined to reply if you didn't post blatant lies.
The code isn't valid python for at least 3 separate reasons, so to
claim it doesn't give you errors is just nonsense.

Tet
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug

dudes dudes | 2 Oct 11:05 2011
Picon

Re: matching strings in python

thanks Ted :)
I've only sent a small part of the code and removed parts not needed to be shown.
You could've just ignored my email rather been rude :)


> To: gllug <at> gllug.org.uk
> From: sta296 <at> astradyne.co.uk
> Date: Sun, 2 Oct 2011 09:45:44 +0100
> Subject: Re: [Gllug] matching strings in python
>
>
> dudes dudes writes:
>
> >/*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
>
> I might be more inclined to reply if you didn't post blatant lies.
> The code isn't valid python for at least 3 separate reasons, so to
> claim it doesn't give you errors is just nonsense.
>
> Tet
> --
> Gllug mailing list - Gllug <at> gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Richard W.M. Jones | 2 Oct 11:28 2011

Re: matching strings in python

On Sun, Oct 02, 2011 at 09:36:08AM +0100, dudes dudes wrote:
>    if re.match('(10000)' | '(10001)', line)

Test that the program you are posting compiles first.  Avoids wasting
everyone else's time on something that you could have fixed yourself.

You probably want to match on: "1000[01]".

Rich.

--

-- 
Richard Jones
Red Hat
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug

Stuart Sears | 2 Oct 11:33 2011

Re: matching strings in python

On 02/10/11 09:36, dudes dudes wrote:
> Hello,
> 
> Can someone kindly point me my mistake here in this code. I'm trying to
> search a file for 10000 OR 10001 (in python).
> 
> import re;
> 
> this= open("file", r)
> for line in this:
>    if re.match('(10000)' | '(10001)', line)
>         print line

RTFM :)

http://docs.python.org/library/re.html#matching-vs-searching

specifically:
where are these numbers on each line? Are they at the start? if not,
re.match won't find them without a preceding wildcard.

Also, there's no need for the 'this=...' bit, unless you actually feel
you need the variable.

Something like the following

for line in open('filename'):
    if re.search('1000[01]', line):
        print line

is more likely to succeed

> /*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
As Tet says, that's difficult to credit, unless you have selectively
copied and pasted random bits of your code into this email and changed
the quoting. Or just attempted to write it in email from memory.

At the very least:

* the 'r' on your open(...) line is not quoted.

* The re.match line itself will throw at least one syntax error - all
characters in your pattern should be inside one set of quotes.

* The second if statement is missing a colon.

This example was fairly trivial, but if you are asking for help with
code like this, it would help if you actually post working excerpts from
it. Or excerpts that throw the errors you are asking about.

Regards,

Stuart
--

-- 
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug

John Edwards | 2 Oct 11:29 2011
Picon

Re: matching strings in python

On Sun, Oct 02, 2011 at 10:05:57AM +0100, dudes dudes wrote:
> 
> thanks Ted :)
> I've only sent a small part of the code and removed parts not needed to be shown. 
> You could've just ignored my email rather been rude :)

He's trying to tell you that your code will not run, and for
reasons that have nothing to do with what you think is wrong.

Have you even tried to run that section of code through python?

If you try your code one step at a time, you'll find that even
your open line has a bug (missing quotes around r):
	http://docs.python.org/library/functions.html#open

You should also fix how you read in the contents of the file:
	http://docs.python.org/library/stdtypes.html#file-objects

Then when you've fixed those you can read about the python re
module here:
	http://docs.python.org/library/re.html
	http://docs.python.org/howto/regex.html#regex-howto

--

-- 
#---------------------------------------------------------#
|    John Edwards   Email: john <at> cornerstonelinux.co.uk    |
#---------------------------------------------------------#
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
dudes dudes | 2 Oct 11:50 2011
Picon

Re: matching strings in python

John,
I'm using DrPython as an editor on slackware running on V-box. With some strange reason when I try to copy and paste it to my windows, doesn't allow me to that. And for that reason I only "typed" very small parts of the code and during that I have missed " " around r and possibly some other syntaxes. . Now With DrPython, if you  try to save your change, it won't let you to save it if you syntax issues or parts I missing ( such as missing " " around r).

>
> If you try your code one step at a time, you'll find that even
> your open line has a bug (missing quotes around r):
> http://docs.python.org/library/functions.html#open

You mean missing Syntax !
 
I don't understand why all of the sudden everyone think I'm wasting their time !! I just have a deadline for a piece of work that is sue to 1 PM Europe time

thanks John for your links
ak

> Date: Sun, 2 Oct 2011 10:33:38 +0100
> From: stuart <at> sjsears.com
> To: gllug <at> gllug.org.uk
> Subject: Re: [Gllug] matching strings in python
>
> On 02/10/11 09:36, dudes dudes wrote:
> > Hello,
> >
> > Can someone kindly point me my mistake here in this code. I'm trying to
> > search a file for 10000 OR 10001 (in python).
> >
> > import re;
> >
> > this= open("file", r)
> > for line in this:
> > if re.match('(10000)' | '(10001)', line)
> > print line
>
> RTFM :)
>
> http://docs.python.org/library/re.html#matching-vs-searching
>
> specifically:
> where are these numbers on each line? Are they at the start? if not,
> re.match won't find them without a preceding wildcard.
>
> Also, there's no need for the 'this=...' bit, unless you actually feel
> you need the variable.
>
> Something like the following
>
> for line in open('filename'):
> if re.search('1000[01]', line):
> print line
>
> is more likely to succeed
>
>
> > /*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
> As Tet says, that's difficult to credit, unless you have selectively
> copied and pasted random bits of your code into this email and changed
> the quoting. Or just attempted to write it in email from memory.
>
> At the very least:
>
> * the 'r' on your open(...) line is not quoted.
>
> * The re.match line itself will throw at least one syntax error - all
> characters in your pattern should be inside one set of quotes.
>
> * The second if statement is missing a colon.
>
> This example was fairly trivial, but if you are asking for help with
> code like this, it would help if you actually post working excerpts from
> it. Or excerpts that throw the errors you are asking about.
>
> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
> --
> Gllug mailing list - Gllug <at> gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Vaidas Jablonskis | 2 Oct 12:04 2011
Picon

Re: matching strings in python

re.search('10000|10001', line)

On 2 October 2011 10:50, dudes dudes <akam123 <at> hotmail.co.uk> wrote:
John,
I'm using DrPython as an editor on slackware running on V-box. With some strange reason when I try to copy and paste it to my windows, doesn't allow me to that. And for that reason I only "typed" very small parts of the code and during that I have missed " " around r and possibly some other syntaxes. . Now With DrPython, if you  try to save your change, it won't let you to save it if you syntax issues or parts I missing ( such as missing " " around r).

>
> If you try your code one step at a time, you'll find that even
> your open line has a bug (missing quotes around r):
> http://docs.python.org/library/functions.html#open

You mean missing Syntax !
 
I don't understand why all of the sudden everyone think I'm wasting their time !! I just have a deadline for a piece of work that is sue to 1 PM Europe time

thanks John for your links
ak

> Date: Sun, 2 Oct 2011 10:33:38 +0100
> From: stuart <at> sjsears.com
> To: gllug <at> gllug.org.uk

> Subject: Re: [Gllug] matching strings in python
>
> On 02/10/11 09:36, dudes dudes wrote:
> > Hello,
> >
> > Can someone kindly point me my mistake here in this code. I'm trying to
> > search a file for 10000 OR 10001 (in python).
> >
> > import re;
> >
> > this= open("file", r)
> > for line in this:
> > if re.match('(10000)' | '(10001)', line)
> > print line
>
> RTFM :)
>
> http://docs.python.org/library/re.html#matching-vs-searching
>
> specifically:
> where are these numbers on each line? Are they at the start? if not,
> re.match won't find them without a preceding wildcard.
>
> Also, there's no need for the 'this=...' bit, unless you actually feel
> you need the variable.
>
> Something like the following
>
> for line in open('filename'):
> if re.search('1000[01]', line):
> print line
>
> is more likely to succeed
>
>
> > /*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
> As Tet says, that's difficult to credit, unless you have selectively
> copied and pasted random bits of your code into this email and changed
> the quoting. Or just attempted to write it in email from memory.
>
> At the very least:
>
> * the 'r' on your open(...) line is not quoted.
>
> * The re.match line itself will throw at least one syntax error - all
> characters in your pattern should be inside one set of quotes.
>
> * The second if statement is missing a colon.
>
> This example was fairly trivial, but if you are asking for help with
> code like this, it would help if you actually post working excerpts from
> it. Or excerpts that throw the errors you are asking about.
>
> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
> --
> Gllug mailing list - Gllug <at> gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug

--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




--
Vaidas Jablonskis

--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
dudes dudes | 2 Oct 12:08 2011
Picon

Re: matching strings in python



>
> RTFM :)

TFM has been Read b4 starting the project :P

> http://docs.python.org/library/re.html#matching-vs-searching
>
> specifically:
> where are these numbers on each line? Are they at the start? if not,
> re.match won't find them without a preceding wildcard.

yes there are one each line and at the start of each
 
> Also, there's no need for the 'this=...' bit, unless you actually feel
> you need the variable.

yes I need "this" as variable even though it has a different variable name in the source code.
 
> Something like the following
>
> for line in open('filename'):
> if re.search('1000[01]', line):
> print line

 
> is more likely to succeed

YES True, but there are 10.000 different digit numbers, but I only need to find  10 numbers .. such as 10000, 10001, 1286, 3412, 3512, 456, 653,,,etc and they are different. Therefore I need to retrieve one out of these 10 numbers !
>
> > /*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
> As Tet says, that's difficult to credit, unless you have selectively
> copied and pasted random bits of your code into this email and changed
> the quoting. Or just attempted to write it in email from memory.

copy and paste doesn't work due to my platform environment settings ! I have typed some part and yes I missed some syntax !
> At the very least:
>
> * the 'r' on your open(...) line is not quoted
> * The re.match line itself will throw at least one syntax error - all
> characters in your pattern should be inside one set of quotes.

yes True but using DrPython as an editor  won't let you to save the code if you have syntax issues.

> * The second if statement is missing a colon.
>
> This example was fairly trivial, but if you are asking for help with
> code like this, it would help if you actually post working excerpts from
> it. Or excerpts that throw the errors you are asking about.

agree and thank you :)

> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
> --
> Gllug mailing list - Gllug <at> gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
dudes dudes | 2 Oct 12:14 2011
Picon

Re: matching strings in python

worked :) thanks
and with re.match ( ) too

From: jablonskis <at> gmail.com
Date: Sun, 2 Oct 2011 11:04:08 +0100
To: gllug <at> gllug.org.uk
Subject: Re: [Gllug] matching strings in python

re.search('10000|10001', line)

On 2 October 2011 10:50, dudes dudes <akam123 <at> hotmail.co.uk> wrote:
John,
I'm using DrPython as an editor on slackware running on V-box. With some strange reason when I try to copy and paste it to my windows, doesn't allow me to that. And for that reason I only "typed" very small parts of the code and during that I have missed " " around r and possibly some other syntaxes. . Now With DrPython, if you  try to save your change, it won't let you to save it if you syntax issues or parts I missing ( such as missing " " around r).

>
> If you try your code one step at a time, you'll find that even
> your open line has a bug (missing quotes around r):
> http://docs.python.org/library/functions.html#open

You mean missing Syntax !
 
I don't understand why all of the sudden everyone think I'm wasting their time !! I just have a deadline for a piece of work that is sue to 1 PM Europe time

thanks John for your links
ak

> Date: Sun, 2 Oct 2011 10:33:38 +0100
> From: stuart <at> sjsears.com
> To: gllug <at> gllug.org.uk

> Subject: Re: [Gllug] matching strings in python
>
> On 02/10/11 09:36, dudes dudes wrote:
> > Hello,
> >
> > Can someone kindly point me my mistake here in this code. I'm trying to
> > search a file for 10000 OR 10001 (in python).
> >
> > import re;
> >
> > this= open("file", r)
> > for line in this:
> > if re.match('(10000)' | '(10001)', line)
> > print line
>
> RTFM :)
>
> http://docs.python.org/library/re.html#matching-vs-searching
>
> specifically:
> where are these numbers on each line? Are they at the start? if not,
> re.match won't find them without a preceding wildcard.
>
> Also, there's no need for the 'this=...' bit, unless you actually feel
> you need the variable.
>
> Something like the following
>
> for line in open('filename'):
> if re.search('1000[01]', line):
> print line
>
> is more likely to succeed
>
>
> > /*It doesn't give me any errors. However; doesn't find the 10000 OR 10001 */
> As Tet says, that's difficult to credit, unless you have selectively
> copied and pasted random bits of your code into this email and changed
> the quoting. Or just attempted to write it in email from memory.
>
> At the very least:
>
> * the 'r' on your open(...) line is not quoted.
>
> * The re.match line itself will throw at least one syntax error - all
> characters in your pattern should be inside one set of quotes.
>
> * The second if statement is missing a colon.
>
> This example was fairly trivial, but if you are asking for help with
> code like this, it would help if you actually post working excerpts from
> it. Or excerpts that throw the errors you are asking about.
>
> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
> --
> Gllug mailing list - Gllug <at> gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug

--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




--
Vaidas Jablonskis


-- Gllug mailing list - Gllug <at> gllug.org.uk http://lists.gllug.org.uk/mailman/listinfo/gllug
--
Gllug mailing list  -  Gllug <at> gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug

Gmane