Skip to content

FIX: extend access to quotechar param in DataFrame.to_csv #6034

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ New features

- Added ``date_format`` and ``datetime_format`` attribute to ExcelWriter.
(:issue:`4133`)
- ``DataFrame.to_csv`` now accepts ``quotechar`` parameter (:issue:`6034`)

API Changes
~~~~~~~~~~~
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ def to_panel(self):
def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
cols=None, header=True, index=True, index_label=None,
mode='w', nanRep=None, encoding=None, quoting=None,
line_terminator='\n', chunksize=None,
quotechar='"', line_terminator='\n', chunksize=None,
tupleize_cols=False, date_format=None, **kwds):
r"""Write DataFrame to a comma-separated values (csv) file

Expand Down Expand Up @@ -1109,6 +1109,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
file
quoting : optional constant from csv module
defaults to csv.QUOTE_MINIMAL
quotechar : string (length 1), default '"'
character used to quote fields
chunksize : int or None
rows to write at a time
tupleize_cols : boolean, default False
Expand All @@ -1129,8 +1131,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
float_format=float_format, cols=cols,
header=header, index=index,
index_label=index_label, mode=mode,
chunksize=chunksize, engine=kwds.get(
"engine"),
chunksize=chunksize, quotechar=quotechar,
engine=kwds.get("engine"),
tupleize_cols=tupleize_cols,
date_format=date_format)
formatter.save()
Expand Down
22 changes: 20 additions & 2 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,10 +1669,10 @@ def test_to_latex(self):
\end{tabular}
"""
self.assertEqual(withoutindex_result, withoutindex_expected)

def test_to_latex_escape_special_chars(self):
special_characters = ['&','%','$','#','_',
'{','}','~','^','\\']
'{','}','~','^','\\']
df = DataFrame(data=special_characters)
observed = df.to_latex()
expected = r"""\begin{tabular}{ll}
Expand All @@ -1694,6 +1694,24 @@ def test_to_latex_escape_special_chars(self):
"""
self.assertEqual(observed, expected)

def test_to_csv_quotechar(self):
df = DataFrame({'col' : [1,2]})
expected = """\
$$,$col$
$0$,$1$
$1$,$2$
"""
from tempfile import NamedTemporaryFile
with NamedTemporaryFile() as output:
df.to_csv(output.name, quoting=1, quotechar="$") # QUOTE_ALL
results = open(output.name).read()
self.assertEqual(results, expected)

df.to_csv(output.name, quoting=1, quotechar="$", engine='python')
results = open(output.name).read()
self.assertEqual(results, expected)


class TestSeriesFormatting(tm.TestCase):
_multiprocess_can_split_ = True

Expand Down