Skip to content
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

Allow empty/Excel files by handling 'None' encoding #228

Merged
merged 2 commits into from
Oct 18, 2018
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
Empty file added data/empty.csv
Empty file.
6 changes: 4 additions & 2 deletions datapackage/resource.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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