Skip to content

BUG: CSV – escaped quotes are not correctly unescaped on reading a CSV file #56690

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

Closed
2 of 3 tasks
macwinnie opened this issue Dec 31, 2023 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@macwinnie
Copy link

macwinnie commented Dec 31, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import io
import pandas as pd

test_csv = '''"test";"columns"
"This is a """TEST"""";"with """escaped""" quotes"
'''

data = pd.read_csv(
    io.StringIO( test_csv ),
    delimiter=";",
)

csv = {}
for c in data.columns:
    csv[c] = data.get(c).to_list()

assert csv["test"][0] == 'This is a "TEST"'
assert csv["columns"][0] == 'with "escaped" quotes'

Issue Description

The value with escaped quotes This is a """TEST""" should be transferred correctly to This is a "TEST", but it is not: It seems like only the first occurrence of tripple-quotes in a column / cell is correctly handled and replaced ...

so ... instead of the required assertions in the example above, those assertions are true in the current pandas version(s):

assert csv["test"][0] == 'This is a "TEST""""'
assert csv["columns"][0] == 'with "escaped""" quotes"'

Even the column closing quote is not removed correctly when escaped quotes are part of a cell ...

Expected Behavior

Assert should not fail – so the escaped quotes should be handled correctly.

Installed Versions

MacOS Sonoma:

INSTALLED VERSIONS

commit : a671b5a
python : 3.11.6.final.0
python-bits : 64
OS : Darwin
OS-release : 23.2.0
Version : Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:34 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8103
machine : arm64
processor : arm
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.4
numpy : 1.26.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.4
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

python:latest Container (running on Ubuntu Server):

INSTALLED VERSIONS

commit : a671b5a
python : 3.12.1.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-169-generic
Version : #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.1.4
numpy : 1.26.2
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.2
pip : 23.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

@macwinnie macwinnie added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 31, 2023
@macwinnie
Copy link
Author

Playing around with those escaped quotes goes crazy even more:

import io
import math
import pandas as pd

test_csv = '''"test";"columns"
"This is a """TEST"""";"with """escaped""" quotes"
"Unclosed """ quote";"in first cell of row"
"Single quote"""";"at the end of first cell"
'''

data = pd.read_csv(
    io.StringIO( test_csv ),
    delimiter=";",
)

csv = {}
for c in data.columns:
    csv[c] = data.get(c).to_list()


# currently true assertions:

assert csv["test"][1] == 'Unclosed " quote"'

assert csv["test"][2] == 'Single quote"";at the end of first cell"'
assert math.isnan(csv["columns"][2])


# expected true assertions:

assert csv["test"][1] == 'Unclosed " quote'

assert csv["test"][2] == 'Single quote"'
assert csv["columns"][2] == 'at the end of first cell'

@asishm
Copy link
Contributor

asishm commented Dec 31, 2023

fwiw python stdlib csv gives the same behavior as pandas (c and pyarrow engines) for your first post. However the pandas python engine raises ParserError: ';' expected after '"'

Imo you have an extra set of double quotes in there. with

test_csv = '''"test";"columns"
"This is a ""TEST""";"with ""escaped"" quotes"
'''

which generates

               test                columns
0  This is a "TEST"  with "escaped" quotes

With your 2nd example, both the python and the pyarrow engines raise and pandas c engine matches stdlib csv module.

@macwinnie
Copy link
Author

Hmm ... you're totally right @asishm ... I doublechecked with RFC 4180 ... and escaping quotes has to be by prepending it with another (one, single) double quote ...

That's annoying that there are different versions running around in the web – some other references talk about escaping special characters by quoting them again in quotes which would result in """ for " ... but yeah ... double checking resources ... my fault =D

Happy new year ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants