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

ENH: line_terminator parameter for DataFrame.to_csv #2383

Closed
wants to merge 4 commits 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
8 changes: 5 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,

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):
mode='w', nanRep=None, encoding=None, quoting=None, line_terminator='\n'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep an eye on line length (re: PEP8)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I totally missed that. If it makes sense, I'm going to re-organise the developers' page to make the workflow explicit (move the testing and flake8 sections right before 'pushing your changes').

"""
Write DataFrame to a comma-separated values (csv) file

Expand Down Expand Up @@ -1336,6 +1336,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
encoding : string, optional
a string representing the encoding to use if the contents are
non-ascii, for python versions prior to 3
line_terminator: string, default '\n'
The newline character or character sequence to use in the output file
"""
if nanRep is not None: # pragma: no cover
import warnings
Expand All @@ -1355,11 +1357,11 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,

try:
if encoding is not None:
csvout = com.UnicodeWriter(f, lineterminator='\n',
csvout = com.UnicodeWriter(f, lineterminator=line_terminator,
delimiter=sep, encoding=encoding,
quoting=quoting)
else:
csvout = csv.writer(f, lineterminator='\n', delimiter=sep,
csvout = csv.writer(f, lineterminator=line_terminator, delimiter=sep,
quoting=quoting)
self._helper_csvexcel(csvout, na_rep=na_rep,
float_format=float_format, cols=cols,
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,29 @@ def test_to_csv_index_no_leading_comma(self):
'three,3,6\n')
self.assertEqual(buf.getvalue(), expected)

def test_to_csv_line_terminators(self):
df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
index=['one', 'two', 'three'])

buf = StringIO()
df.to_csv(buf, line_terminator='\r\n')
expected = (',A,B\r\n'
'one,1,4\r\n'
'two,2,5\r\n'
'three,3,6\r\n')
self.assertEqual(buf.getvalue(), expected)

buf = StringIO()
df.to_csv(buf) # The default line terminator remains \n
expected = (',A,B\n'
'one,1,4\n'
'two,2,5\n'
'three,3,6\n')
self.assertEqual(buf.getvalue(), expected)




def test_to_excel_from_excel(self):
try:
import xlwt
Expand Down