Skip to content

Commit

Permalink
Allow empty/Excel files by handling 'None' encoding (#228)
Browse files Browse the repository at this point in the history
* Allow empty files, handle 'None' encoding

* Only set encoding if encoding was detected
  • Loading branch information
akariv authored and roll committed Oct 18, 2018
1 parent bb79bbf commit cf56286
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Empty file added data/empty.csv
Empty file.
6 changes: 4 additions & 2 deletions datapackage/resource.py
Expand Up @@ -253,8 +253,10 @@ def infer(self, **options):
for chunk in stream:
contents += chunk
if len(contents) > 1000: break
encoding = cchardet.detect(contents)['encoding'].lower()
descriptor['encoding'] = 'utf-8' if encoding == 'ascii' else encoding
encoding = cchardet.detect(contents)['encoding']
if encoding is not None:
encoding = encoding.lower()
descriptor['encoding'] = 'utf-8' if encoding == 'ascii' else encoding

# Schema
if not descriptor.get('schema'):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_infer.py
Expand Up @@ -29,3 +29,7 @@ def test_infer():
def test_infer_non_utf8_file():
descriptor = infer('data/data_with_accents.csv')
assert descriptor['resources'][0]['encoding'] == 'iso-8859-1'

def test_infer_empty_file():
descriptor = infer('data/empty.csv')
assert descriptor['resources'][0].get('encoding') is None

0 comments on commit cf56286

Please sign in to comment.