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

Why is the escape_character flag determined by the doubleQuote property of a dialect? #852

Open
xrotwang opened this issue Jun 19, 2017 · 0 comments

Comments

@xrotwang
Copy link

I'm trying to convert dialect descriptions to the equivalent set of formatting parameters in python. It appears that this is not possible, because naively translating a "doubleQuote": true to doublequote=True, ecapechar='"' in python will fail as exemplified with this CSV:

COL1,COL2
"quoted, "" content",val2

and the snippet of python 3 below. Note that setting escapechar to '\\' indepent of doublequote succeeds.

import csv

csv.field_size_limit(20)  # this limits the maximal field length to just a bit above what would be required in our example

with open('escape_char.csv') as fp:
    print(list(csv.reader(fp, escapechar='\\', doublequote=True))[:2])

with open('escape_char.csv') as fp:
    print(list(csv.reader(fp, escapechar='"', doublequote=True))[:2])

The python code succeeds for the first attempt at reading and fails for the second:

$ python3 escape_char.py 
[['COL1', 'COL2'], ['quoted, " content', 'val2']]
Traceback (most recent call last):
  File "escape_char.py", line 9, in <module>
    print(list(csv.reader(fp, escapechar='"', doublequote=True))[:2])
_csv.Error: field larger than field limit (20)
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

1 participant