Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #329 from alphagov/convert-excel-float-to-int
Browse files Browse the repository at this point in the history
Attempt to convert excel floats to int
  • Loading branch information
jabley committed Jul 28, 2014
2 parents c61bc84 + 3243629 commit 5415cd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion backdrop/core/upload/parse_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ def _extract_cell_value(cell, book):
if cell.ctype == xlrd.XL_CELL_DATE:
time_tuple = xlrd.xldate_as_tuple(cell.value, book.datemode)
return utc(datetime.datetime(*time_tuple)).isoformat()
if cell.ctype == xlrd.XL_CELL_EMPTY:
elif cell.ctype == xlrd.XL_CELL_EMPTY:
return None
elif cell.ctype == xlrd.XL_CELL_NUMBER:
# Convert to int where possible to match CSV parsing
return int(cell.value) if cell.value % 1 == 0 else cell.value
elif cell.ctype == xlrd.XL_CELL_ERROR:
logging.warn("Encountered errors in cells when parsing excel file")
return EXCEL_ERROR
Expand Down
12 changes: 8 additions & 4 deletions tests/core/upload/test_parse_excel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import unittest
from hamcrest import assert_that, only_contains, contains
from backdrop.core.errors import ParseError
from hamcrest import assert_that, contains, instance_of

from backdrop.core.upload.parse_excel import parse_excel, ExcelError, EXCEL_ERROR
from tests.support.test_helpers import fixture_path, d_tz
from backdrop.core.upload.parse_excel import parse_excel, EXCEL_ERROR
from tests.support.test_helpers import fixture_path


class ParseExcelTestCase(unittest.TestCase):
Expand Down Expand Up @@ -59,3 +58,8 @@ def test_parse_xlsx_handle_empty_cells_and_lines(self):
[None, None, None],
["The above row", "is full", "of nones"]
)))

def test_that_numbers_are_converted_to_int_where_possible(self):
data = map(list, self._parse_excel("xlsfile.xls"))

assert_that(data[0][1][2], instance_of(int))

0 comments on commit 5415cd0

Please sign in to comment.