django-rest-framework-csv icon indicating copy to clipboard operation
django-rest-framework-csv copied to clipboard

newline handling in restframework csv

Open drake01 opened this issue 6 years ago • 4 comments

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?

drake01 avatar Oct 10 '19 16:10 drake01

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'}]

cvn avatar Oct 12 '19 11:10 cvn

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

drake01 avatar Oct 13 '19 12:10 drake01

Looks like this is a duplicate of #24.

cvn avatar Oct 14 '19 03:10 cvn

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.

drake01 avatar Oct 22 '20 17:10 drake01