HOWTO · Python
How to Write Array to CSV File in Python [4 Ways]
Learn to write arrays to CSV files in Python with this comprehensive guide. Covering four methods, including NumPy, Pandas, CSV module, and file-handling techniques. Perfect for those looking to master write array to csv python.
On this page
CSV (comma-separated values) stores tabular data as rows of text, so it is useful for sharing processed arrays with spreadsheets and other programs. Choose NumPy for array-shaped data, Pandas when you already work with a DataFrame, and Python’s csv module for general rows and reliable quoting. The examples below use commas as delimiters and show how the resulting rows are read.
Write a NumPy array to CSV with numpy.savetxt()
numpy.savetxt() writes one- or two-dimensional arrays to a text file. Pass delimiter="," to separate columns with commas; its default delimiter is a space, which does not produce comma-separated columns. For a one-dimensional numeric array, use np.savetxt("numbers.csv", data, delimiter=",", fmt="%d") when integer text is suitable. For decimal values, choose a format such as "%.2f" to control displayed precision.
For tabular data with text and numbers, create a two-dimensional array such as np.array([["Name", "Age"], ["Ada", 36]]) and pass fmt="%s". NumPy arrays have one element type, so combining text and numbers produces string values; the format writes those values as text. CSV itself does not preserve Python or NumPy types. A program reading the file must interpret values such as 36 if it needs an integer.
NumPy writes each array row as a CSV row. It does not add column names unless you supply a header; by default, a supplied header is prefixed with a comment marker. For a plain first row of column names, set header="Name,Age" and comments="".
Write an array to CSV with Pandas DataFrame.to_csv()
Convert array-like data to a Pandas DataFrame when you want labeled columns or already use Pandas. For example, df = pd.DataFrame(data, columns=["Name", "Age"]) gives the values column names, and df.to_csv("people.csv", index=False) writes them. The index=False option prevents Pandas from adding row labels as an extra first column; omit it only when those labels belong in the file. Set header=False if the destination should not include column names. See the DataFrame.to_csv() reference for its other export options.
Write rows to CSV with Python’s csv.writer()
Python’s standard-library csv.writer() is a good general choice for rows containing text, numbers, commas, or quotes. Open the file with newline="", create a writer, and pass rows to writerows(). The writer handles CSV quoting when a value contains a comma or quote. This example uses only the standard library and prints the saved file contents so the row and delimiter behavior is visible:
Run this example:
import csv
from pathlib import Path
from tempfile import TemporaryDirectory
rows = [[1, 4, 2], [7, 9, 4], [0, 6, 2]]
with TemporaryDirectory() as directory:
path = Path(directory) / "numbers.csv"
with path.open("w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerows(rows)
print(path.read_text(encoding="utf-8"), end="")
1,4,2
7,9,4
0,6,2
For large inputs, writerows() accepts an iterable of rows, so the entire dataset need not be assembled into another list. The file is closed when its with block ends.
Read a CSV file back with csv.reader()
csv.reader() reads the file and yields each row as a list of strings. It is a reader, not a writer object. For instance, a cell written as 36 is read as the string "36"; convert it with int() or float() when numeric operations are needed. Open text files for CSV reading with newline="" as well. If a file has a header, handle its first row separately, for example by calling next(reader) before processing the data rows. The reader also interprets quoted delimiters, unlike splitting each line on commas.
Write simple rows with Python file handling
Direct file handling is adequate for simple, controlled values that cannot contain commas, quotes, or line breaks. Convert each value to text, join a row with commas, and end it with a newline—for example, file.write(",".join(map(str, row)) + "\n"). This is manual text formatting, not a general CSV serializer: a name such as "Doe, Jane" would be split into two columns. Use csv.writer() for arbitrary data so quoting and delimiters are handled correctly. Use newline="" when opening a file for CSV output, and choose an explicit encoding such as UTF-8 when the file will contain non-ASCII text.