regular expression

Discussion in 'Python' started by Wiebke Pätzold, Jul 31, 2003.

  1. Hi all,

    I wrote a little program. Here it is:

    import sys
    import Mk4py

    db = Mk4py.storage("c:\\datafile.mk",1)
    vw = db.view("people")

    def func(row):
    try:
    if row.Nachname[0:1]=='G':
    return 1
    else:
    return 0
    except AttributeError:
    return 0


    vf = vw.filter(func)

    for r in vf:
    print vw[r.index].Nachname,vw[r.index].Kongressbereich


    I create a database that contains a table. 'Nachname' and
    'Kongressbereich' are special fieldnames. This program can search for
    a special letter. In my example it is 'G'. and the search takes place
    in 'Nachname'.
    Mow I want to use regular expression. So that I can limit my search.
    For example: I can search for Ge and it is not relevant wich letters
    follow
    Could somebody help me with this task?

    Wiebke
    Wiebke Pätzold, Jul 31, 2003
    #1
    1. Advertising

  2. Wiebke Pätzold wrote:

    > On Thu, 31 Jul 2003 14:24:01 +0200, Thomas Güttler
    > <> wrote:
    >
    >>Wiebke Pätzold wrote:


    Please cut unimportant parts if you reply.

    > Can you tell me please on wich place in my program the if-statement
    > will be inserted?


    At the same place where you have it (if ...=="G")

    > And then I have another question too. After the if statement there
    > must be a print statement. I have no idea how I must write my program
    > so that it is right and executably.
    > Can you write it me exactly how this program should look like?


    You seem to be running on windows ("C:\..."). I think double-clicking
    on it should execute it.

    If you want to make it executable without python installed: Have a look
    at py2exe.

    Since you seem to be a german: Maybe my Python introduction helps you:
    http://www.thomas-guettler.de/vortraege/python/einfuehrung.html

    thomas
    Thomas =?ISO-8859-15?Q?G=FCttler?=, Jul 31, 2003
    #2
    1. Advertising

  3. On Thu, 31 Jul 2003 10:46:59 -0500, Jeff Epler <>
    wrote:

    >These are all untested, because I don't have Mk4py or your datafile to
    >try it on.
    >
    >I might write this:
    > import re
    > pattern = re.compile("^Ge")
    > def func(row):
    > try:
    > nachname = row.Nachname
    > except AttributeError:
    > return 0
    > return pattern.search(nachname) is not None
    >
    > vf = vw.filter(func)
    >
    >If you're using a Python version with nested scopes, you could use them
    >in this case:
    > import re
    > def make_func(pattern):
    > pattern = re.compile(pattern)
    > def func(row):
    > try:
    > nachname = row.Nachname
    > except AttributeError:
    > return 0
    > return pattern.search(nachname) is not None
    > return func
    >
    > vf = vw.filter(make_func("^Ge"))
    >
    >or you can make a callable filter object by using classes:
    > import re
    > class PatternFilter:
    > def __init__(self, pattern):
    > self.pattern = re.compile(pattern)
    >
    > def __call__(self, row):
    > try:
    > nachname = row.Nachname
    > except AttributeError:
    > return 0
    > return self.pattern.search(Nachname) is not None
    > vf = vw.filter(PatternFilter("^Ge"))
    >
    >Jeff


    thank you for the part of the program.
    But I have one question.
    If I insert your first suggestion- there is print nothin. Is it
    possible that there is missing something?
    Wiebke Pätzold, Aug 1, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    441
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,274
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    832
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,924
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page