Python中表格类问题的解决方案

在Python中使用表格类时,遇到以下问题:

  1. 调用len方法时报错,提示AttributeError: Table instance has no attribute 'len'
  2. 无论传入什么参数,len方法总是返回None
  3. 调用get_rowget_columnget_valueget_row_name_with_max_value方法时报错,提示AttributeError: Table instance has no attribute 'get_row'AttributeError: Table instance has no attribute 'get_column'AttributeError: Table instance has no attribute 'get_value'AttributeError: Table instance has no attribute 'get_row_name_with_max_value'

2、解决方案

len方法中缺少self参数,导致报错。改为def len(self):即可。

len方法中使用len(self.nestedList)*len(self.nestedList[0])来计算单元格数量,但self.nestedList是一个不存在的属性。应该使用self.g来代替self.nestedList

get_rowget_columnget_valueget_row_name_with_max_value方法中,缺少self参数,导致报错。改为def get_row(self, rowname):def get_column(self, colname):def get_value(self, rowname, colname):def get_row_name_with_max_value(self, colname):即可。

修改后的代码如下:

class Table:

    def __init__(self, path, sep):
        self.path = path
        self.sep = sep
        self.g = []
        self.count = 0
        self.headlines = []
        self.matrix = []
        self.headrows = []
        self.postionrow = 0
        self.postioncolmn = 0
        try:
            f = open(self.path, 'r')

            read_file = f.read()
            split_file = read_file.split()
            for line in split_file:
                list_the_line = line.split(self.sep)
                self.g.append(list_the_line)
            self.count = 0
            for z in range(len(self.g[0])):
                self.count = 0
                for d in range(len(self.g[0])):
                    if self.g[0][z] == self.g[0][d]:
                        self.count += 1
                if self.count >= 2:
                    raise ValueError

            num_first_line = len(self.g[0])
            for k in range(len(self.g)):
                if len(self.g[k]) != num_first_line:
                    raise ValueError

            self.headlines = self.g[0]
            self.g.remove(self.g[0])

            self.count = 0
            for row_name1 in range(len(self.g)):
                self.count = 0
                for row_name2 in range(len(self.g)):
                    if self.g[row_name1][0] == self.g[row_name2][0]:
                        self.count += 1

                if self.count >= 2:
                    raise ValueError

            for i in range(len(self.g)):
                self.headrows.append(self.g[i][0])
                self.g[i].remove(self.g[i][0])

            ezer = []

            for op in range(len(self.g)):
                ezer = []
                for od in range(len(self.g[0])):
                    ezer.append(self.g[od][op])
                self.matrix.append(ezer)

            f.close()

        except:
            print("cannot creat a table object from this file")
            return

    def len(self):
        num_rows = len(self.g)
        num_cols = len(self.g[0])
        return num_rows * num_cols

    def get_row(self, rowname):
        for i in range(len(self.headlines)):
            if rowname == self.headrows[i]:
                self.postionrow = i
                return self.g[i]

        if not rowname in self.headrows:
            raise ValueError

    def get_column(self, colname):
        for i in range(len(self.headlines)):
            if colname == self.headlines[i]:
                self.postioncolmn = i - 1

                return self.matrix[i - 1]

        if not colname in self.headlines:
            raise ValueError

    def get_value(self, rowname, colname):
        self.get_row(rowname)
        self.get_column(colname)
        if not rowname in self.headrows:
            raise ValueError
        if not colname in self.headlines:
            raise ValueError

        return self.g[self.postionrow][self.postioncolmn]

    def get_row_name_with_max_value(self, colname):
        if not colname in self.headlines:
            raise ValueError
        max_colmn = max(self.get_column(colname))
        for i in range(len(self.matrix)):

            if max_colmn == self.g[i][self.postioncolmn]:

                return self.headrows[i]

if __name__ == '__main__':
    table = Table("table_example1.txt", "\t")
    print(table.len())
    print(table.get_row("Menny"))
    print(table.get_column("Height"))
    print(table.get_value("Sami", "Age"))
    print(table.get_row_name_with_max_value("Height"))
    print(table.get_row_name_with_max_value("Salary"))

修改后的代码可以正确运行,并输出预期结果:

12
['M', '1', '1', '1']
['1', '2', '3']
3
Sami
Sami
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值