diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0a57e54..53e473e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,11 @@ Change log 0.6.0 - tbd -------------------------------------------------------------------------------- +**added** + +#. `#86 `_: allow trailing + options, get_data(...keep_trailing_empty_cells=True). + **fixed** #. `#74 `_: handle zip files diff --git a/changelog.yml b/changelog.yml index 42f52b2..ea6d4bd 100644 --- a/changelog.yml +++ b/changelog.yml @@ -2,6 +2,9 @@ name: pyexcel-io organisation: pyexcel releases: - changes: + - action: added + details: + - "`#86`: allow trailing options, get_data(...keep_trailing_empty_cells=True)." - action: fixed details: - "`#74`: handle zip files which contain non-UTF-8 encoded files." diff --git a/docs/source/index.rst b/docs/source/index.rst index bd24745..49f216e 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -91,6 +91,7 @@ get_data(.., library='pyexcel-ods') csvz sqlalchemy django + options extensions diff --git a/docs/source/options.rst b/docs/source/options.rst new file mode 100644 index 0000000..9ef9db4 --- /dev/null +++ b/docs/source/options.rst @@ -0,0 +1,11 @@ +Options +====================== + +Here is the documentation on the keyword options for get_data. + +keep_trailing_empty_cells +------------------------------ + +default: False + +If turned on, the return data will contain trailing empty cells. diff --git a/pyexcel_io/reader.py b/pyexcel_io/reader.py index 49d08c3..0a1a5ea 100644 --- a/pyexcel_io/reader.py +++ b/pyexcel_io/reader.py @@ -17,6 +17,7 @@ def clean_keywords(keywords): "skip_row_func", "skip_empty_rows", "row_renderer", + "keep_trailing_empty_cells", ] for arg in keywords: if arg in args_list: diff --git a/pyexcel_io/sheet.py b/pyexcel_io/sheet.py index 5afd982..b23f342 100644 --- a/pyexcel_io/sheet.py +++ b/pyexcel_io/sheet.py @@ -38,6 +38,7 @@ def __init__( skip_column_func=None, skip_empty_rows=False, row_renderer=None, + keep_trailing_empty_cells=False, **deprecated_use_of_keywords_here ): self._native_sheet = sheet @@ -51,6 +52,7 @@ def __init__( self._skip_column = _index_filter self._skip_empty_rows = skip_empty_rows self._row_renderer = row_renderer + self.keep_trailing_empty_cells = keep_trailing_empty_cells if skip_row_func: self._skip_row = skip_row_func @@ -84,10 +86,13 @@ def to_array(self): elif column_position == constants.STOP_ITERATION: break - tmp_row.append(cell_value) - if cell_value is not None and cell_value != "": - return_row += tmp_row - tmp_row = [] + if self.keep_trailing_empty_cells: + return_row.append(cell_value) + else: + tmp_row.append(cell_value) + if cell_value is not None and cell_value != "": + return_row += tmp_row + tmp_row = [] if self._skip_empty_rows and len(return_row) < 1: # we by-pass next yeild here # because it is an empty row diff --git a/tests/test_csv_book.py b/tests/test_csv_book.py index 76acfbe..9b64915 100644 --- a/tests/test_csv_book.py +++ b/tests/test_csv_book.py @@ -3,6 +3,7 @@ from unittest import TestCase import pyexcel_io.manager as manager +from pyexcel_io import get_data from pyexcel_io.sheet import NamedContent from pyexcel_io.reader import EncapsulatedSheetReader from pyexcel_io._compact import BytesIO, StringIO @@ -115,6 +116,20 @@ def test_sheet_file_reader(self): result = list(r.to_array()) self.assertEqual(result, [[1], [4, 5, 6], ["", 7]]) + def test_sheet_file_reader_with_trailing_empty_cells(self): + r = EncapsulatedSheetReader( + CSVFileReader(NamedContent(self.file_type, self.test_file)), + keep_trailing_empty_cells=True, + ) + result = list(r.to_array()) + self.assertEqual(result, [[1], [4, 5, 6, "", ""], ["", 7]]) + + def test_get_data_with_trailing_empty_cells(self): + result = get_data(self.test_file, keep_trailing_empty_cells=True) + self.assertEqual( + result[self.test_file], [[1], [4, 5, 6, "", ""], ["", 7]] + ) + def tearDown(self): os.unlink(self.test_file)