regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wiebke Pätzold

    regular expression

    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].Kongressbereic h


    I create a database that contains a table. 'Nachname' and
    'Kongressbereic h' 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
  • Thomas Güttler

    #2
    Re: regular expression

    Wiebke Pätzold wrote:
    [color=blue]
    > On Thu, 31 Jul 2003 14:24:01 +0200, Thomas Güttler
    > <guettler@thoma s-guettler.de> wrote:
    >[color=green]
    >>Wiebke Pätzold wrote:[/color][/color]

    Please cut unimportant parts if you reply.
    [color=blue]
    > Can you tell me please on wich place in my program the if-statement
    > will be inserted?[/color]

    At the same place where you have it (if ...=="G")
    [color=blue]
    > 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?[/color]

    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:


    thomas


    Comment

    • Wiebke Pätzold

      #3
      Re: regular expression

      On Thu, 31 Jul 2003 10:46:59 -0500, Jeff Epler <jepler@unpytho nic.net>
      wrote:
      [color=blue]
      >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(patte rn):
      > pattern = re.compile(patt ern)
      > 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(patt ern)
      >
      > def __call__(self, row):
      > try:
      > nachname = row.Nachname
      > except AttributeError:
      > return 0
      > return self.pattern.se arch(Nachname) is not None
      > vf = vw.filter(Patte rnFilter("^Ge") )
      >
      >Jeff[/color]

      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?

      Comment

      Working...