【问题标题】:TypeError: 'headers' is an invalid keyword argument for print()TypeError:'headers' 是 print() 的无效关键字参数
【发布时间】:2019-09-06 14:08:55
【问题描述】:

我最近在 conda 上安装了制表,我正在尝试使用打印语法将我的结果制表 来源:Printing Lists as Tabular Data 但我收到“TypeError:'headers' is an invalid keyword argument for print()”

我试过“print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age'], tablefmt='orgtbl'))”

from tabulate import tabulate
i: int
with open("incre.txt", "w") as file:

    for i in range(1, 100,5):
        mol = int((i*50)/(i+50))
        file.write(str(i)+ " " +str(mol) + "\n")
    print(tabulate([[i], [mol]]), headers=['i' , 'mol'], tablefmt='orgtbl')
    file.close()

预期结果将基于

我收到类型错误,我在这里遗漏了什么?

【问题讨论】:

    标签: python-3.x tabulate


    【解决方案1】:

    你写括号的方式有误,试试那行:

    print(tabulate([[i], [mol]], headers=['i' , 'mol'], tablefmt='orgtbl'))
    

    你所做的就像这样做:

    x = tabulate([[i], [mol]]
    print(x, headers=['i' , 'mol'], tablefmt='orgtbl')
    

    如您所见,您尝试使用 headerstablefmt 关键字调用 print 方法,导致错误:'headers' is an invalid keyword argument for print()

    更新:

    我不确定,但我认为您尝试实现的是:

    from tabulate import tabulate
    
    values = []
    
    for i in range(1, 100,5):
        mol = int((i*50)/(i+50))
        values.append([i, mol])
    
    print(tabulate(values, headers=['i' , 'mol'], tablefmt='orgtbl'))
    

    在您的代码中,您在退出 while 循环后打印了 imol,那么您将只打印它们的最后一个值...

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 2020-07-13
      • 2021-12-25
      • 1970-01-01
      • 2019-05-17
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多