Skip to content

Commit

Permalink
🎉 allow trailing options, get_data(...keep_trailing_empty_cells=True).
Browse files Browse the repository at this point in the history
…fix #86
  • Loading branch information
chfw committed Sep 30, 2020
1 parent efcf49c commit 168e89e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ get_data(.., library='pyexcel-ods')
csvz
sqlalchemy
django
options
extensions


Expand Down
11 changes: 11 additions & 0 deletions docs/source/options.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions pyexcel_io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions pyexcel_io/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_csv_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +116,18 @@ 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)

Expand Down

0 comments on commit 168e89e

Please sign in to comment.