Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support Mac OS LF char for csv #517

@matthewhegarty

Description

@matthewhegarty

Hi
I'm testing some changes on django-import-export and I notice an issue with LF endings for csv data, which was the format on older mac os distributions.

>>> import tablib
>>> sb = 'id,name,author_email\r1,Some book,[email protected]\r'
>>> tablib.import_set(sb, format='csv')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/matthew/.virtualenvs/django-import-export/lib/python3.9/site-packages/tablib/core.py", line 908, in import_set
    return Dataset().load(normalize_input(stream), format, **kwargs)
  File "/home/matthew/.virtualenvs/django-import-export/lib/python3.9/site-packages/tablib/core.py", line 414, in load
    fmt.import_set(self, stream, **kwargs)
  File "/home/matthew/.virtualenvs/django-import-export/lib/python3.9/site-packages/tablib/formats/_csv.py", line 44, in import_set
    for i, row in enumerate(rows):
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

This can be fixed by adding the 'newline' kwarg to normalize_input():

def normalize_input(stream):
    """
    Accept either a str/bytes stream or a file-like object and always return a
    file-like object.
    """
    if isinstance(stream, str):
        return StringIO(stream, newline='')
    elif isinstance(stream, bytes):
        return BytesIO(stream)
    return stream
  • tablib: 3.2.0
  • python 3.9.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions