объект 'function' не может быть подписан, когда параметр является списком - PullRequest
0 голосов
/ 17 марта 2020

Поэтому мне нужно, чтобы мой вывод выглядел так: output

Последний вопрос будет таким: Как мне go добавить значения каждого столбца в строку строки?

Я определяю функцию, которая форматирует способ печати строк, она выглядит следующим образом:

def line_string(row_title, row_data, data_type):

    row_title = f"{row_title:38}"
    row_string = row_title   

    columns = years[-1]

    for column in range(columns+1):

        # Concatenate row_string per the data_type function arugment as per below
        # Make each of the data columns 9 characters wide

        # If data_type equals "decimal", then format as decimal with the specified width

        if data_type == "integer":
            row_string = row_string + f"{row_data[column]:9,}"

        elif data_type == "decimal":
            row_string = row_string + f"{row_data[column]:9,.2f}"

        elif data_type == "percent":
            row_string = row_string + f"{row_data[column]:9,.2%}"

    return row_string 

# this is a test:

line_string("Cumulative sum", cumulatitve_sum, "decimal") 

Я получаю эту ошибку, хотя:


TypeError                                 Traceback (most recent call last)
 in 
     44     return row_string
     45 
---> 46 line_string("Cumulative sum", cumulatitve_sum, "decimal")

 in line_string(row_title, row_data, data_type)

     35         elif data_type == "decimal":
---> 36             row_string = row_string + f"{row_data[column]:9,.2f}"
     37 

TypeError: 'function' object is not subscriptable

Как мне go добавить значения каждого столбца в строку?

1 Ответ

0 голосов
/ 17 марта 2020

Я очень тупой, и я сделал опечатку.

"Возможно, вы имеете в виду даже просто line_string (" Накопительная сумма ", cumulatitve_sum ()," десятичная ") - кодзиро 1 час go"

Да, это совершенно верно.

...