help with looping, re.search, multiple indexing

Discussion in 'Python' started by Lance Hoffmeyer, Feb 16, 2007.

  1. Hey all,

    How do I add another index inside a re.search?

    I want to add
    for j in [96,97,98,99]
    BASE = re.search ...
    sh.Cells97...
    for i ....

    so that the Table 96 within the re.search(s) becomes Table 96, Table 97, Table 98, Table 99
    but don't know how to add a second index within the re.search since I already have %char
    in match = re.search ... " %char, neuro ...


    VAR=[]
    BASE = re.search("Table 96.*?BASE.*?\d(.*?)\n", neuro, re.S ).group(1).split()[1]
    sh.Cells(7,lastcol+1).Value = BASE

    for i, char in enumerate(["RECEIVED E", "PARTICIPATED "]):
    match = re.search(r"Table 96.*?%s.*?\n.*?\d(.*?)\n.*?" %char , neuro, re.S )
    if match:
    VAR = match.group(1).split()[1]
    else:
    VAR = 0
    if VAR=="-": VAR = 0
    VAR = even_odd_round(float(str(VAR)))
    VAR = float(str(VAR))/100
    sh.Cells(i+8,lastcol+1).Value = VAR
    sh.Cells(10,lastcol+1).Value= sh.Cells(9,lastcol+1).Value
    sh.Cells(9,lastcol+1).Value = sh.Cells(8,lastcol+1).Value - sh.Cells(10,lastcol+1).Value



    VAR=[]
    BASE = re.search("Table 97.*?BASE.*?\d(.*?)\n", neuro, re.S ).group(1).split()[1]
    sh.Cells(11,lastcol+1).Value = BASE
    for i, char in enumerate(["RECEIVED E", "PARTICIPATED "]):
    match = re.search(r"Table 97.*?%s.*?\n.*?\d(.*?)\n.*?" %char , neuro, re.S )
    if match:
    VAR = match.group(1).split()[1]
    else:
    VAR = 0
    if VAR=="-": VAR = 0
    VAR = even_odd_round(float(str(VAR)))
    VAR = float(str(VAR))/100
    sh.Cells(i+12,lastcol+1).Value = VAR

    sh.Cells(14,lastcol+1).Value= sh.Cells(13,lastcol+1).Value
    sh.Cells(13,lastcol+1).Value = sh.Cells(12,lastcol+1).Value - sh.Cells(14,lastcol+1).Value
     
    Lance Hoffmeyer, Feb 16, 2007
    #1
    1. Advertisements

  2. Lance Hoffmeyer

    Peter Otten Guest

    You can make format strings with an arbitrary number of values by providing
    a tuple

    r"Table %d.*?%s.*?\n.*?\d(.*?)\n.*?" % (j, char)

    or a dict

    r"Table %(table)d.*?%(char)s.*?\n.*?\d(.*?)\n.*?" % dict(table=j, char=char)

    on the right side of the % operator.

    http://docs.python.org/lib/typesseq-strings.html

    Peter
     
    Peter Otten, Feb 16, 2007
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.