How to Set the Line Ending of a CSV File in pandas with lineterminator
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
df.to_csv(<path>, lineterminator=<ending>)
| Part | What it means |
|---|---|
df | The 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
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())

repr(os.linesep)prints this system's line ending, with the invisible characters written out as\nor\r\n.index=Falseleaves out the row numbers, so each file holds just the two columns.- The three
to_csvcalls differ only inlineterminator: none,"\n"and"\r\n". open(name, "rb")opens each file in binary mode, sofile.read()returns its exact bytes, line endings included, without Python changing them.
Step 2: Run It
uv run python line_endings.py
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'

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, sodefault.csvcame out the same asunix.csv. windows.csvends 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:
import pandas as pd
scores = pd.DataFrame({"name": ["Ana", "Ben"], "score": [71, 64]})
scores.to_csv("old.csv", index=False, line_terminator="\n")
uv run python old_name.py
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'?

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
| Mistake | What happens | Do this instead |
|---|---|---|
line_terminator="\n" | TypeError: … unexpected keyword argument 'line_terminator' | lineterminator="\n" |
| Leaving the default for a file that must be the same everywhere | The file's bytes depend on the system that wrote it | Set 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\n | Open it with newline="", as in the FAQ |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
got an unexpected keyword argument 'line_terminator' | The argument's old name | Rename it to lineterminator |
| The same code writes different files on different computers | to_csv uses each system's line ending | Set 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:
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())
uv run python open_file.py
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
- pandas.DataFrame.to_csv:
lineterminatorand every other argument. - What's new in 1.5.0: why
line_terminatorbecamelineterminator.
