简体   繁体   中英

Python: use a list index as a function argument

I'm trying to use list indices as arguments for a function that performs regex searches and substitutions over some text files. The different search patterns have been assigned to variables and I've put the variables in a list that I want to feed the function as it loops through a given text.

When I call the function using a list index as an argument nothing happens (the program runs, but no substitutions are made in my text files), however, I know the rest of the code is working because if I call the function with any of the search variables individually it behaves as expected.

When I give the print function the same list index as I'm trying to use to call my function it prints exactly what I'm trying to give as my function argument, so I'm stumped!


search1 = re.compile(r'pattern1')
search2 = re.compile(r'pattern2')
search3 = re.compile(r'pattern3')

searches = ['search1', 'search2', 'search2']
i = 0

for …
  …
  def fun(find)
    …

  fun(searches[i])
  if i <= 2:
    i += 1  
…

As mentioned, if I use fun(search1) the script edits my text files as wished. Likewise, if I add the line print(searches[i]) it prints search1 (etc.), which is what I'm trying to give as an argument to fun .

Being new to Python and programming, I've a limited investigative skill set, but after poking around as best I could and subsequently running print(searches.index(search1) and getting a pattern1 is not in list error, my leading (and only) theory is that I'm giving my function the actual regex expression rather than the variable it's stored in???

Much thanks for any forthcoming help!

Try to changes your searches list to be [search1, search2, search3] instead of ['search1', 'search2', 'search2'] (in which you just use strings and not regex objects)

Thanks to all for the help. eyl327's comment that I should use a list or dictionary to store my regular expressions pointed me in the right direction.

However, because I was using regex in my search patterns, I couldn't get it to work until I also created a list of compiled expressions (discovered via this thread on stored regex strings ).

Very appreciative of juanpa.arrivillaga point that I should have proved a MRE (please forgive, with a highly limited skill set, this in itself can be hard to do), I'll just give an excerpt of a slightly amended version of my actual code demonstrating the answer (one again, please forgive its long-windedness, I'm not presently able to do anything more elegant):


…

# put regex search patterns in a list
rawExps = ['search pattern 1', 'search pattern 2', 'search pattern 3']
# create a new list of compiled search patterns 
compiledExps = [regex.compile(expression, regex.V1) for expression in rawExps]

i = 0
storID = 0
newText = ""

for file in filepathList:
    for expression in compiledExps:
        with open(file, 'r') as text:
            thisText = text.read()
            lines = thisThis.splitlines()
            setStorID = regex.search(compiledExps[i], thisText)
            if setStorID is not None:
                storID = int(setStorID.group())
            for line in lines:
                def idSub(find):
                    global storID
                    global newText
                    match = regex.search(find, line)
                    if match is not None:
                        newLine = regex.sub(find, str(storID), line) + "\n"
                        newText = newText + newLine
                        storID = plus1(int(storID), 1)
                    else:
                        newLine = line + "\n"
                        newText = newText + newLine
                # list index number can be used as an argument in the function call
                idSub(compiledExps[i])
            if i <= 2:
                i += 1
        write()
        newText = ""
    i = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM