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

.read_csv() isn't reading escape characters properly #25501

Open
jelther opened this issue Mar 1, 2019 · 0 comments
Open

.read_csv() isn't reading escape characters properly #25501

jelther opened this issue Mar 1, 2019 · 0 comments
Labels
Bug IO CSV read_csv, to_csv

Comments

@jelther
Copy link

jelther commented Mar 1, 2019

Code Sample, a copy-pastable example if possible

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO

import pandas as pd
import csv

escape_char_defined = '\\'
quote_defined = '"'
separator = "|"

sample_data = []

for i in range(1,11):
    sample_data.append(i*escape_char_defined + quote_defined)

initial_df = pd.DataFrame(sample_data,columns=['column'])


csv_text = initial_df.to_csv(sep=separator,columns=None,header=None,index=False,doublequote=False,quoting=csv.QUOTE_ALL,quotechar=quote_defined,escapechar=escape_char_defined,encoding='utf-8')

csv_text = StringIO(csv_text)

final_df = pd.read_csv(csv_text,sep=separator,escapechar=escape_char_defined,quoting=csv.QUOTE_ALL,header=None,doublequote=False,encoding='utf-8')

if not final_df.equals(initial_df):
    raise Exception("Dataframes are not equal!")    

Problem description

When defining several strings with an escape char before the quotechar, the same CSV generated by pandas is not read properly when transforming back to a pandas dataframe.

Expected Output

The dataframe written should be equal to the dataframe read.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.24.1
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.5.12
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml.etree: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.18
pymysql: None
psycopg2: None
jinja2: 2.8.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jbrockmendel jbrockmendel added the IO CSV read_csv, to_csv label Jul 23, 2019
@mroeschke mroeschke added the Bug label Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO CSV read_csv, to_csv
Projects
None yet
Development

No branches or pull requests

3 participants