【问题标题】:'function' object is not subscriptable when parameter is a list当参数是列表时,“函数”对象不可下标
【发布时间】:2020-06-28 18:20:15
【问题描述】:

所以我需要我的输出看起来像这样: output

最后一个问题是: 如何将每列的值添加到行字符串?

我正在定义一个函数来格式化行的打印方式,它看起来像这样:

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

如何将每列的值添加到行字符串中?

【问题讨论】:

标签: python function iteration concatenation string-concatenation


【解决方案1】:

我非常愚蠢,我打错了。

“也许你的意思是 line_string("Cumulative sum", cumulatitve_sum(), "decimal") – kojiro 1 小时前"

是的,完全正确。

【讨论】:

    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2019-06-06
    • 2021-10-22
    • 2017-08-08
    • 2021-11-27
    相关资源
    最近更新 更多