Skip to content

Commit 168e89e

Browse files
committed
🎉 allow trailing options, get_data(...keep_trailing_empty_cells=True). fix #86
1 parent efcf49c commit 168e89e

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

changelog.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: pyexcel-io
22
organisation: pyexcel
33
releases:
44
- changes:
5+
- action: added
6+
details:
7+
- "`#86`: allow trailing options, get_data(...keep_trailing_empty_cells=True)."
58
- action: fixed
69
details:
710
- "`#74`: handle zip files which contain non-UTF-8 encoded files."

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ get_data(.., library='pyexcel-ods')
9191
csvz
9292
sqlalchemy
9393
django
94+
options
9495
extensions
9596

9697

docs/source/options.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Options
2+
======================
3+
4+
Here is the documentation on the keyword options for get_data.
5+
6+
keep_trailing_empty_cells
7+
------------------------------
8+
9+
default: False
10+
11+
If turned on, the return data will contain trailing empty cells.

pyexcel_io/reader.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def clean_keywords(keywords):
1717
"skip_row_func",
1818
"skip_empty_rows",
1919
"row_renderer",
20+
"keep_trailing_empty_cells",
2021
]
2122
for arg in keywords:
2223
if arg in args_list:

pyexcel_io/sheet.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(
3838
skip_column_func=None,
3939
skip_empty_rows=False,
4040
row_renderer=None,
41+
keep_trailing_empty_cells=False,
4142
**deprecated_use_of_keywords_here
4243
):
4344
self._native_sheet = sheet
@@ -51,6 +52,7 @@ def __init__(
5152
self._skip_column = _index_filter
5253
self._skip_empty_rows = skip_empty_rows
5354
self._row_renderer = row_renderer
55+
self.keep_trailing_empty_cells = keep_trailing_empty_cells
5456

5557
if skip_row_func:
5658
self._skip_row = skip_row_func
@@ -84,10 +86,13 @@ def to_array(self):
8486
elif column_position == constants.STOP_ITERATION:
8587
break
8688

87-
tmp_row.append(cell_value)
88-
if cell_value is not None and cell_value != "":
89-
return_row += tmp_row
90-
tmp_row = []
89+
if self.keep_trailing_empty_cells:
90+
return_row.append(cell_value)
91+
else:
92+
tmp_row.append(cell_value)
93+
if cell_value is not None and cell_value != "":
94+
return_row += tmp_row
95+
tmp_row = []
9196
if self._skip_empty_rows and len(return_row) < 1:
9297
# we by-pass next yeild here
9398
# because it is an empty row

tests/test_csv_book.py

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from unittest import TestCase
44

55
import pyexcel_io.manager as manager
6+
from pyexcel_io import get_data
67
from pyexcel_io.sheet import NamedContent
78
from pyexcel_io.reader import EncapsulatedSheetReader
89
from pyexcel_io._compact import BytesIO, StringIO
@@ -115,6 +116,18 @@ def test_sheet_file_reader(self):
115116
result = list(r.to_array())
116117
self.assertEqual(result, [[1], [4, 5, 6], ["", 7]])
117118

119+
def test_sheet_file_reader_with_trailing_empty_cells(self):
120+
r = EncapsulatedSheetReader(
121+
CSVFileReader(NamedContent(self.file_type, self.test_file)),
122+
keep_trailing_empty_cells=True,
123+
)
124+
result = list(r.to_array())
125+
self.assertEqual(result, [[1], [4, 5, 6, "", ""], ["", 7]])
126+
127+
def test_get_data_with_trailing_empty_cells(self):
128+
result = get_data(self.test_file, keep_trailing_empty_cells=True)
129+
self.assertEqual(result[self.test_file], [[1], [4, 5, 6, "", ""], ["", 7]])
130+
118131
def tearDown(self):
119132
os.unlink(self.test_file)
120133

0 commit comments

Comments
 (0)