From 55dae2b5436a6c655aeece136998f0cb5abf043e Mon Sep 17 00:00:00 2001 From: Yang Wang <250801860@qq.com> Date: Fri, 17 Mar 2017 10:47:20 +0800 Subject: [PATCH 1/2] Add method del_column. Add an method del_column to delete a column to the table. --- prettytable/prettytable.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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. From e0d162f949a62a4a1f6c81cfe5e6855f902715ca Mon Sep 17 00:00:00 2001 From: Yang Wang <250801860@qq.com> Date: Fri, 17 Mar 2017 10:47:20 +0800 Subject: [PATCH 2/2] Add method del_column. Add an method del_column to delete a column to the table. --- prettytable/prettytable.py | 14 +++++++++++++- tests/test_prettytable.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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"""