简体   繁体   中英

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)

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)

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