【问题标题】:PrettyTable: "if len(self._rows) in (0, len(column)): AttributeError: 'str' object has no attribute '_rows'"PrettyTable:“如果 len(self._rows) in (0, len(column)): AttributeError: 'str' object has no attribute '_rows'”
【发布时间】:2022-01-02 16:37:58
【问题描述】:

我正在尝试使用文档制作一个基本表格。 这是我的代码版本:

from prettytable import *

table = PrettyTable

table.add_column("", "Pokemon Name", ["Pikachu", "Squirtle", "Charmander"]) 
table.add_column("", "Type", ["Electric Type", "Water Type", "Fire Type"])

print(table)

我已尝试删除并重新安装 Prettytable,但问题仍然存在。

if len(self._rows) in (0, len(column)):
AttributeError: 'str' object has no attribute '_rows'

我还能做些什么来解决这个问题吗?

【问题讨论】:

    标签: python-3.x prettytable


    【解决方案1】:

    在 python 中实例化对象的正确方法是 table = PrettyTable()

    然后将您的代码更改为以下内容:

    table.add_column("Pokemon Name", ["Pikachu", "Squirtle", "Charmander"]) 
    table.add_column("Type", ["Electric Type", "Water Type", "Fire Type"])
    
    print(table)
    

    错误来自您将self 传递为"" 的事实

    【讨论】:

    • 天!这帮助很大!
    猜你喜欢
    • 2020-04-26
    • 2017-05-10
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多