M
M
MaxLoL03772020-12-03 01:42:42
Python
MaxLoL0377, 2020-12-03 01:42:42

Could you help with a Python iterator?

Hello! There was a need to include an iterator in your code, but I just ran into a problem. To begin with, I will show the code, then I will try to describe what I want from it.

#Здесь я делаю итератор из файлов в директории
     exel_file = []
 for name in glob.glob('converted_dir/*.xlsx'):
     wb = load_workbook(name)
     list = wb['TDSheet']
     exel_file.append(list)
iter_exel_file = iter(exel_file)
#Функция, которая должна получать поочередно значения из файлов, как раз таки это я и хочу реализовать с помощью итератора
def model_name_fun():
   result = []
 for sheet in iter_exel_file:
    for i in range(19, 70):
        model_name = sheet.cell(row=i, column=2).value
        if model_name is not None:
            result.append(model_name)
return result
#Запрос по Google API, который в моем понимании должен передавать значение так же поочередно, а именно: получил значения из первого файла - передал, получил и так далее
values = service.spreadsheets().values().batchUpdate(
 spreadsheetId=spreadsheet_id,
 body={
     "valueInputOption": "USER_ENTERED",
     "data": [
         {"range": (name_position),
          "majorDimension": "COLUMNS",
          "values": [model_name_fun()]},]} ).execute()

I understand that when iterating it is necessary to use next, but I can not understand where. I ask you to help with advice.

PS Most likely, model_name_fun() and the values ​​request should be enclosed in one function, and for them to substitute values ​​from the iterator, but I also can’t figure out how to do it (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mkone112, 2020-12-03
@mkone112

With indents and formatting in general - complete hell, nothing is clear and few people want to disassemble this mess.

A
Alexander, 2020-12-03
@shabelski89

There's a lot going on, here's an example:

import os

def get_iter_dir(dir):
    return os.listdir(dir)


for i in get_iter_dir(os.getcwd()):
    print(i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question