Skip to content

Commit

Permalink
Improve parsing of escaped double quotes
Browse files Browse the repository at this point in the history
Per the RFC, "" is treated as an escaped quote.
Unfortunately this leads to quoted empty cells, e.g. "","","" being treated as escaped quotes instead of empty cells.

A more careful reading of the spec indicates that this is only valid when already inside double quotes, and the parser has been updated to reflect this.

Also updates the readme to clarify that quoted newlines in cells are not supported.
wadetb committed Nov 13, 2016
1 parent 64d9b35 commit aae9d77
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ Using NumPy (http://www.numpy.org), the plugin supports evaluating Python expres

All the above features work in both justified and collapsed modes.

Finally, the plugin has full support for RFC 4180 quoting.
Finally, the plugin fully supports RFC 4180 (https://tools.ietf.org/html/rfc4180) quoting, with the exception that quoted newlines (2.6) are treated as row separators.

## Install

22 changes: 9 additions & 13 deletions csvplugin.py
Original file line number Diff line number Diff line change
@@ -307,24 +307,20 @@ def ParseRow(self, row):
while char_index < len(row):
char = row[char_index]

if char_index < len(row) - 1:
next_char = row[char_index + 1]
else:
next_char = None

if char == '"' and next_char == '"':
if self.auto_quote:
currentword += '"'
else:
currentword += '""'
char_index += 2
continue

if insidequotes:
if char == '"':
if char_index < len(row) - 1 and row[char_index + 1] == '"':
if self.auto_quote:
currentword += '"'
else:
currentword += '""'
char_index += 2
continue

insidequotes = False
if not self.auto_quote:
currentword += char

else:
currentword += char

4 changes: 4 additions & 0 deletions quote.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a,"b """,456
,b
123abc,"45,6"
"","",""

0 comments on commit aae9d77

Please sign in to comment.