help with looping, re.search, multiple indexing

L

Lance Hoffmeyer

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
 
P

Peter Otten

Lance said:
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 ...

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top