Skip to main content

How to Set the Line Ending of a CSV File in pandas with lineterminator

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

DataFrame.to_csv ends each row of a CSV file with your system's line ending: one character on macOS and Linux, two on Windows. The same data then gives different files on different computers. The lineterminator argument sets the ending yourself, so the file is the same everywhere.

This guide writes one small table three ways and reads the files back byte by byte, with pandas 3.1.2 on Python 3.15.2.

Syntax​

Syntax
df.to_csv(<path>, lineterminator=<ending>)
PartWhat it means
dfThe DataFrame to save
<path>The file to write, such as "scores.csv"
lineterminator=What ends each row. Optional
<ending>"\n", a line feed, which macOS and Linux use, or "\r\n", a carriage return and a line feed, which Windows uses

The pandas documentation describes lineterminator as The newline character or character sequence to use in the output file. Defaults to os.linesep, which depends on the OS in which this method is called ('\n' for linux, '\r\n' for Windows, i.e.). os.linesep is Python's name for the system's line ending.

The Example​

A DataFrame holds two test scores, Ana's and Ben's. A script saves it three times, with the default ending, with "\n" and with "\r\n", then prints each file's raw bytes.

Prerequisites​

  • uv and Python, with pandas in a project (uv add pandas). This guide uses pandas 3.1.2 on Python 3.15.2.

Step 1: Write the Script​

line_endings.py
import os

import pandas as pd

scores = pd.DataFrame({"name": ["Ana", "Ben"], "score": [71, 64]})

print("This system's line ending:", repr(os.linesep))

scores.to_csv("default.csv", index=False)
scores.to_csv("unix.csv", index=False, lineterminator="\n")
scores.to_csv("windows.csv", index=False, lineterminator="\r\n")

for name in ("default.csv", "unix.csv", "windows.csv"):
with open(name, "rb") as file:
print(name, file.read())

line_endings.py in a macOS-style code window: it builds a two-row scores DataFrame, prints os.linesep, saves the DataFrame to default.csv, unix.csv with lineterminator \n and windows.csv with lineterminator \r\n, then prints each file's bytes.

  • repr(os.linesep) prints this system's line ending, with the invisible characters written out as \n or \r\n.
  • index=False leaves out the row numbers, so each file holds just the two columns.
  • The three to_csv calls differ only in lineterminator: none, "\n" and "\r\n".
  • open(name, "rb") opens each file in binary mode, so file.read() returns its exact bytes, line endings included, without Python changing them.

Step 2: Run It​

Terminal
uv run python line_endings.py
Output
This system's line ending: '\n'
default.csv b'name,score\nAna,71\nBen,64\n'
unix.csv b'name,score\nAna,71\nBen,64\n'
windows.csv b'name,score\r\nAna,71\r\nBen,64\r\n'

The terminal after uv run python line_endings.py: this system's line ending is \n, default.csv and unix.csv hold the same bytes with \n after each row, and windows.csv has \r\n after each row.

b'…' shows a file's bytes, with \n and \r standing for the invisible line feed and carriage return:

  • This Mac's line ending is \n, so default.csv came out the same as unix.csv.
  • windows.csv ends each row with \r\n, two bytes, so it's three bytes longer, one for each row.

On Windows, os.linesep is \r\n, so there the default file would match windows.csv instead. Setting lineterminator gives the same file on both. The command works unchanged in PowerShell.

Step 3: See What the Old Name Does​

Code written for pandas versions before 2.0 can spell the argument line_terminator, with an underscore:

old_name.py
import pandas as pd

scores = pd.DataFrame({"name": ["Ana", "Ben"], "score": [71, 64]})
scores.to_csv("old.csv", index=False, line_terminator="\n")
Terminal
uv run python old_name.py
Output
Traceback (most recent call last):
File "/Users/you/source/lang-python/old_name.py", line 4, in <module>
scores.to_csv("old.csv", index=False, line_terminator="\n")
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: NDFrame.to_csv() got an unexpected keyword argument 'line_terminator'. Did you mean 'lineterminator'?

The terminal after uv run python old_name.py: a traceback ending in TypeError: NDFrame.to_csv() got an unexpected keyword argument 'line_terminator'. Did you mean 'lineterminator'?

pandas no longer has that name. The pandas 1.5.0 release notes deprecated it, use lineterminator instead; this is for consistency with read_csv() and the standard library 'csv' module, and pandas 2.0.0 removed it. The end of the error names the fix: drop the underscore.

Common Mistakes​

MistakeWhat happensDo this instead
line_terminator="\n"TypeError: … unexpected keyword argument 'line_terminator'lineterminator="\n"
Leaving the default for a file that must be the same everywhereThe file's bytes depend on the system that wrote itSet lineterminator
Passing a file opened without newline=""On Windows, Python turns each \n it writes into \r\n, so a "\r\n" ending is written as \r\r\nOpen it with newline="", as in the FAQ

Troubleshooting​

SymptomLikely causeFix
got an unexpected keyword argument 'line_terminator'The argument's old nameRename it to lineterminator
The same code writes different files on different computersto_csv uses each system's line endingSet lineterminator

FAQ​

Can I pass an open file instead of a file name?​

Yes. The pandas documentation says a file opened in text mode should be opened with newline='', disabling universal newlines. Python's open() documentation explains why: when writing, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. With newline="", Python writes the endings exactly as pandas gives them:

open_file.py
import pandas as pd

scores = pd.DataFrame({"name": ["Ana", "Ben"], "score": [71, 64]})

with open("opened.csv", "w", encoding="utf-8", newline="") as file:
scores.to_csv(file, index=False, lineterminator="\n")

with open("opened.csv", "rb") as file:
print(file.read())
Terminal
uv run python open_file.py
Output
b'name,score\nAna,71\nBen,64\n'

Which ending should I choose?​

For a file that has to be byte for byte the same on every computer, choose one and set it. "\n" is what macOS and Linux use.

Conclusion​

to_csv ended each row with this Mac's \n by default, lineterminator="\n" gave the same file, and lineterminator="\r\n" gave Windows-style rows, three bytes longer. Setting the argument makes the file the same on every system. The old spelling, line_terminator, fails in pandas 2 and later with a TypeError.

Additional Resources​