Skip to content

readcsv does not handle escaped quotes correctly #3945

Open

Description

@ShreyasKhandekar

Describe the bug
CSV files can have quoted strings. This is usually used to escape the delimiter or other weird characters. Moreover, CSV also allows you to use quotes in your headers or values which can be escaped using a preceding quote ("")

I am referring to the standard laid out in RFC 4180: https://www.ietf.org/rfc/rfc4180.txt

To Reproduce
Steps to reproduce the behavior:

  1. Have a small CSV file which has quotes in the header and/or in the values:
    Ex:
"humidity","light","lpg","motion","smoke","temp","height (in inches)"
"51.0","false","0.00765082227055719","false","0.02041127012241292","22.7","14"""
"76.0","false","0.005114383400977071","false","0.013274836704851536","19.700000762939453","16"""
"50.9","false","0.007673227406398091","false","0.02047512557617824","22.6","25"""

Notice how the height (in inches) is expressed as "14""" where the "" is used to represent an escaped quote so the actual value being represented in 14" where the " stands for inches

  1. Read it in in arkouda:
>>> import arkouda as ak
>>> ak.read_csv("dumdum.csv")
{'\\"humidity\\"': array(['"51.0"', '"76.0"', '"50.9"']), '\\"light\\"': array(['"false"', '"false"', '"false"']), '\\"lpg\\"': array(['"0.00765082227055719"', '"0.005114383400977071"', '"0.007673227406398091"']), '\\"motion\\"': array(['"false"', '"false"', '"false"']), '\\"smoke\\"': array(['"0.02041127012241292"', '"0.013274836704851536"', '"0.02047512557617824"']), '\\"temp\\"': array(['"22.7"', '"19.700000762939453"', '"22.6"']), '\\"height (in inches)\\"': array(['"14"""', '"16"""', '"25"""'])}

Notice the weird escape patterns in the header, and the unnecessary quotes being included in the values

Or see how it looks as a DataFrame

>>> ak.DataFrame.read_csv("dumdum.csv")
  \"humidity\" \"light\"  ...              \"temp\" \"height (in inches)\"
0       "51.0"   "false"  ...                "22.7"                 "14"""
1       "76.0"   "false"  ...  "19.700000762939453"                 "16"""
2       "50.9"   "false"  ...                "22.6"                 "25""" (3 rows x 7 columns)

Expected behavior
The quotes, unless escaped with "", should be excluded from the values.

See how Pandas does it:

>>> import pandas as pd
>>> pd.read_csv("dumdum.csv")
   humidity  light       lpg  motion     smoke       temp height (in inches)
0      51.0  False  0.007651   False  0.020411  22.700000                14"
1      76.0  False  0.005114   False  0.013275  19.700001                16"
2      50.9  False  0.007673   False  0.020475  22.600000                25"

In fact, pandas goes a step further to also recognize the boolean values false and true and convert them to the python False and True, and cast the appropriate string to numbers, etc.

Is this a Blocking Issue
People could potentially preprocess their data to remove quotes, but they really should not need to do this, especially if the quotes are needed to manage cases where a value contains the column delimiter.

  • Are there any known work-arounds? No

Additional context

There is a challenge to maintain correctness without sacrificing performance. Advancing through the column delimiter in Chapel is fast and efficient, but when we try to take into account maintain the "state" of being in quotes and managing them to ignore column delimiters makes this slower.
This becomes even slower when escaped quotes can exist which require marking and reverting the file pointer to manage reads.

Activity

  1. added
    bug
    File IO
    on Dec 13, 2024
  2. changed the title [-]`readcsv` does not handle quoted strings correctly[/-] [+]`readcsv` does not handle escaped quotes correctly[/+] on Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

File IObug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions