Skip to content

allow trailing options, get_data(...keep_trailing_empty_cells=… #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 30, 2020
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Change log
0.6.0 - tbd
--------------------------------------------------------------------------------

**added**

#. `#86 <https://github.com/pyexcel/pyexcel-io/issues/86>`_: allow trailing
options, get_data(...keep_trailing_empty_cells=True).

**fixed**

#. `#74 <https://github.com/pyexcel/pyexcel-io/issues/74>`_: handle zip files
Expand Down
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
15 changes: 15 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,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)

Expand Down