You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
importioimportpandasaspdtest_csv='''"test";"columns""This is a """TEST"""";"with """escaped""" quotes"'''data=pd.read_csv(
io.StringIO( test_csv ),
delimiter=";",
)
csv= {}
forcindata.columns:
csv[c] =data.get(c).to_list()
assertcsv["test"][0] =='This is a "TEST"'assertcsv["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):
assertcsv["test"][0] =='This is a "TEST""""'assertcsv["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
Playing around with those escaped quotes goes crazy even more:
importioimportmathimportpandasaspdtest_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= {}
forcindata.columns:
csv[c] =data.get(c).to_list()
# currently true assertions:assertcsv["test"][1] =='Unclosed " quote"'assertcsv["test"][2] =='Single quote"";at the end of first cell"'assertmath.isnan(csv["columns"][2])
# expected true assertions:assertcsv["test"][1] =='Unclosed " quote'assertcsv["test"][2] =='Single quote"'assertcsv["columns"][2] =='at the end of first cell'
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
testcolumns0Thisisa"TEST"with"escaped"quotes
With your 2nd example, both the python and the pyarrow engines raise and pandas c engine matches stdlib csv module.
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
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
Issue Description
The value with escaped quotes
This is a """TEST"""
should be transferred correctly toThis 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):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
The text was updated successfully, but these errors were encountered: