Reputation: 911
I am making a Table object where you can see the columns and rows. When I try updating the table, the table is not updated properly. Why is this?
This is my current code:
class Table:
def __init__(self, headers, rows):
self.headers, self.rows = list(headers), list(rows)
self.content = [self.headers,
[i for i in self.rows]]
def show(self):
print(str(list(self.headers)).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", ""))
for i in self.rows:
tp = str(i).replace(", ", " | ").replace("'", "").replace("[", "").replace("]", "")
print(tp)
def add(self, headers=None, rows=None):
if headers:
self.headers.extend(headers)
if rows:
self.rows.extend(rows)
def update(self):
self.content = [self.headers,
[i for i in self.rows]]
t = Table(["a", "b"], [[1, 2, 3], [4, 5, 6]])
t.show()
print("-"*20)
t.add("c", [7, 8, 9])
t.update()
t.show()
Can anybody help me?
Upvotes: 0
Views: 30
Reputation: 3910
As I said in the comments I'm not quite sure about the expected output. But if it is:
a | b | c
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Then you have to put additional brackets in your call to add
method. It is expecting a list of list as its second argument as far as I can tell. So:
t.add("c", [[7, 8, 9]])
That does the job.
Upvotes: 1