Search code examples
pythonfor-loopenumerate

Why is the value of index always zero when "variable_names" returns multiple results?


I want to count the results in each regex search against a file. I believe I'm populating a list and then looping through trying to get the value of a counter.

for file in XDTS:
    data_tag_regex = re.compile(r'data_tag=\"(.*?)\"')
    if file.endswith('.xdt'):
        xdt_file = open(file, 'r')
        for line in xdt_file:
            variable_names = data_tag_regex.findall(line)
            for index, variable_name in enumerate(variable_names):
                print(index)

Solution

  • You have one match per line, and multiple lines match. Your enumerate() call is starting from 0 each time because it is a new call for each new line:

    for line in xdt_file:
        # per line, find matches
        variable_names = data_tag_regex.findall(line)
        # for *this line only* print the indices, counting from 0
        for index, variable_name in enumerate(variable_names):
            print(index)
    

    If you wanted to keep an index per match across all lines, you need to count independently:

    index = 0
    for line in xdt_file:
        variable_names = data_tag_regex.findall(line)
        for variable_name in variable_names:
            index += 1
            print(index)