diff --git a/prettytable/prettytable.py b/prettytable/prettytable.py index 1f9e3a1..4a37e03 100644 --- a/prettytable/prettytable.py +++ b/prettytable/prettytable.py @@ -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. diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 6d8c573..65b08f4 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -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"""