Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,19 @@ def del_row(self, row_index):
if row_index > len(self._rows) - 1:
raise Exception("Cant delete row at index %d, table only has %d rows!" % (row_index, len(self._rows)))
del self._rows[row_index]


def del_column(self, column_index):
"""Delete a column to the table

Arguments:
column_index - The index of the column you want to delete. Indexing starts at 0."""

if column_index > len(self._field_names) - 1:
raise Exception("Cant delete column at index %d, table only has %d columns!" % (column_index, len(self._field_names)))
else:
self._field_names.pop(column_index)
self._rows = [row[:column_index] + row[column_index+1:] for row in self._rows]

def add_column(self, fieldname, column, align="c", valign="t"):

"""Add a column to the table.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ def setUp(self):
BasicTests.setUp(self)
self.x.int_format = "04"

class DelColumnTests(CityDataTest):
"""Test del_column function"""
def testDelColumn(self):
self.x.del_column(3)
self.assertTrue(["City name", "Area", "Population"] == self.x.field_names)
string = self.x.get_string()
assert "600.5" not in string
assert "1146.4" not in string
assert "112" in string
assert "1714.7" not in string

class FloatFormatBasicTests(BasicTests):
"""Run the basic tests after setting a float format string"""
Expand Down