在Python中使用表格类时,遇到以下问题:
- 调用
len
方法时报错,提示AttributeError: Table instance has no attribute 'len'
。 - 无论传入什么参数,
len
方法总是返回None
。 - 调用
get_row
、get_column
、get_value
、get_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_row
、get_column
、get_value
、get_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