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

newline handling in restframework csv #82

Open
drake01 opened this issue Oct 10, 2019 · 4 comments
Open

newline handling in restframework csv #82

drake01 opened this issue Oct 10, 2019 · 4 comments

Comments

@drake01
Copy link

drake01 commented Oct 10, 2019

Hi,
As I understand csv parser removes newline characters here: https://github.com/mjumbewu/django-rest-framework-csv/blob/master/rest_framework_csv/parsers.py#L25-L26

Lets say there a cell in csv where value is something like:
'''
Nature
Foliage
Trees
'''

Now we want to parse this particular cell as ["Nature", "Foliage", "Trees"] But with current parser code we can't? because after splitlines() the cell becomes "NatureFoliageTrees"?

Is there a way to handle the issue I am facing?

@cvn
Copy link

cvn commented Oct 12, 2019

I work with @drake01. Here's an example file, saved from Excel for Mac:

newline-example.csv.zip

kingdom,keywords
plant,"leaves
roots
flowers"

In use:

>>> print(CSVParser().parse(data))

# expected/desired output
[{'kingdom': 'plant', 'keywords': 'leaves\nroots\nflowers'}]

# actual output
[{'kingdom': 'plant', 'keywords': 'leavesrootsflowers'}]

@drake01
Copy link
Author

drake01 commented Oct 13, 2019

Thanks a lot for this example @cvn.
This one is more detailed and covers more cases.

@cvn
Copy link

cvn commented Oct 14, 2019

Looks like this is a duplicate of #24.

@drake01
Copy link
Author

drake01 commented Oct 22, 2020

We were able to create new Parser class derived from rest_framework_csv.parsers.CSVParser and override parse method.

In parse method, stream data can be split using: stream.splitlines(keepends=True)

Anyone facing this issue can use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants