简体   繁体   中英

Increment column i element by values inside list

I have a loop that searches keywords in a text file and pastes integers that follow the keywords into an excel file. I want the integers to be in specific cells in the excel file. Is it possible to increment i by the values in i_list rather than always 5 like in the example?

i_list = [5,3,1,1]

def search(file, excel_file):
    i = 2
    found_keywords = list()
    wb = load_workbook(excel_file)
    sheets = wb.sheetnames
    sheet1 = wb[sheets[0]]

    for kwrd in keywords:
        for line in file:
            if kwrd in line and kwrd not in found_keywords:
                found_keywords.append(kwrd)
                sheet1.cell(row=3, column=i).value = int(re.search(r"\d+", line).group())
                i += 5
            elif kwrd in line:
                continue

    wb.save(excel_file)

This code snippet, adjusted from the original, cycles through the values of i_list :

i_list = [5,3,1,1]

def search(file, excel_file):
    i = 2
    found_keywords = list()
    wb = load_workbook(excel_file)
    sheets = wb.sheetnames
    sheet1 = wb[sheets[0]]

    for kwrd in keywords:
        for line in file:
            if kwrd in line and kwrd not in found_keywords:
                for i in i_list: # Update i based on the i-list value
                    sheet1.cell(row=3, column=i).value = int(re.search(r"\d+", line).group())
            elif kwrd in line:
                continue
    wb.save(excel_file)

If you dont need to cycle through the values of i then you can just create a generator to return you the values of i one by one. I have wrapped the calling of the next value of i in a try block since once you run out of values the code wouldnt know what to do. so we break the loop

i_list = (i for i in [5,3,1,1])
i = 2
for _ in range(10):
    print(i)
    try:
        i += next(i_list)
    except StopIteration as si:
        print("no more values in i so loop terminating")
        break

OUTPUT

2
7
10
11
12
no more values in i so loop terminating

However if you want to cycle thorugh the values of i you can use cycle from itertools module and infintly take the next item from i_list for as long as you need

from itertools import cycle
i_list = cycle([5,3,1,1])
i = 2
for _ in range(10):
    print(i)
    i += next(i_list)

OUTPUT

2
7
10
11
12
17
20
21
22
27

UPDATE OF YOUR CODE

below is an update of your code based on the fact you said you dont have to cycle. Remember that once you reach the end of i_list your code will not be able to increase i since there are no more values in i_list.

i_list = [5,3,1,1]
i_generator = (i for i in i_list)

def search(file, excel_file):
    i = 2
    found_keywords = list()
    wb = load_workbook(excel_file)
    sheets = wb.sheetnames
    sheet1 = wb[sheets[0]]

    for kwrd in keywords:
        for line in file:
            if kwrd in line and kwrd not in found_keywords:
                found_keywords.append(kwrd)
                sheet1.cell(row=3, column=i).value = int(re.search(r"\d+", line).group())
                i += next(i_generator)
            elif kwrd in line:
                continue

    wb.save(excel_file)

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