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

Python csv does a better job with escape characters and quotes than CleverCSV #130

Closed
seperman opened this issue Jun 14, 2024 · 1 comment

Comments

@seperman
Copy link

seperman commented Jun 14, 2024

Hello,
First of all, thank you for CleverCSV. I use it mainly as a replacement of Python csv. However, I noticed that Python's csv does a better job at handling escape characters and quoes:

Please consider the following:

import clevercsv
import csv
from io import StringIO

data = """sku,features,attributes
22221,"[{""key"":""heel_height"",""value"":""Ulttra High (4\\""+)""}]","11,room"
"""

print("Python csv")

stream = StringIO(data)

reader = csv.reader(stream, delimiter=',', quotechar='"', escapechar='\\')

for row in reader:
    print(row)

# ---------------

print("clever csv")

stream = StringIO(data)

for row in clevercsv.reader(stream, delimiter=',', quotechar='"', escapechar='\\'):
    print(row)

This will print:

Python csv
['sku', 'features', 'attributes']
['22221', '[{"key":"heel_height","value":"Ulttra High (4"+)""}]"', '11,room']
clever csv
['sku', 'features', 'attributes']
['22221', '"[{"key":"heel_height","value":"Ulttra High (4""+)""}]","11', 'room"\n']

Clever CSV splits the line in the wrong place. It also convert the \" into "" which is not correct.

@GjjvdBurg
Copy link
Collaborator

Hi @seperman thanks for opening this issue. I've taken a look and I think in this particular case the problem is with the dialect, not the parsing. If we don't set the escape character then the text is parsed correctly, and I've added a test in 4b4082a to illustrate this. Please reopen the issue if that doesn't resolve your problem.

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