🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.savetxt()

AI & DATA SCIENCE // np-savetxt

np.savetxt() writes an array to a plain text file, one row per line, with values separated by a delimiter (a comma, by default whitespace).

Syntax

np.savetxt(file, arr, delimiter=' ', fmt='%.18e')

Deep Dive Course

Unlike np.save()'s binary .npy format, savetxt() produces a human-readable text file, making it suitable for exporting data to be inspected manually, opened as a CSV in a spreadsheet, or read by tools outside the NumPy/Python ecosystem — but at the cost of larger file sizes and potential precision loss, since every number gets converted to a fixed-precision text representation via the fmt parameter, and rereading that text can't always recover the exact original binary floating-point value. savetxt() only supports 1D and 2D arrays; higher-dimensional arrays need to be reshaped or saved a different way.

1Understanding np.savetxt()

Unlike np.save()'s binary .npy format, savetxt() produces a human-readable text file, making it suitable for exporting data to be inspected manually, opened as a CSV in a spreadsheet, or read by tools outside the NumPy/Python ecosystem — but at the cost of larger file sizes and potential precision loss, since every number gets converted to a fixed-precision text representation via the fmt parameter, and rereading that text can't always recover the exact original binary floating-point value. savetxt() only supports 1D and 2D arrays; higher-dimensional arrays need to be reshaped or saved a different way.

💡

savetxt() converts numbers to text using a fixed format string, which can silently lose some floating-point precision — use np.save() instead of savetxt() whenever exact, lossless round-tripping of the data matters more than human readability or interoperability.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")

with open("data.csv") as f:
    print(f.read())
localhost:3000

2Practical Example

Here is a real-world application of np.savetxt() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([3.14159265358979, 2.71828182845905])
np.savetxt("precise.txt", arr, fmt="%.4f")

with open("precise.txt") as f:
    print(f.read())
localhost:3000

3Best Practices

Follow these guidelines when working with np.savetxt():

1. Use savetxt() specifically when the output needs to be human-readable, opened in a spreadsheet, or read by a non-NumPy tool

2. Set an appropriate fmt string explicitly when the default scientific-notation formatting isn't what you want for the output file

3. Use np.save() instead of savetxt() when exact precision and fast round-tripping matter more than readability or external tool compatibility

⚠️

Tip: savetxt() converts numbers to text using a fixed format string, which can silently lose some floating-point precision — use np.save() instead of savetxt() whenever exact, lossless round-tripping of the data matters more than human readability or interoperability.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")

with open("data.csv") as f:
    print(f.read())
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")

with open("data.csv") as f:
    print(f.read())
Example 02Advanced Example
import numpy as np

arr = np.array([3.14159265358979, 2.71828182845905])
np.savetxt("precise.txt", arr, fmt="%.4f")

with open("precise.txt") as f:
    print(f.read())

Best Practices

  • Use savetxt() specifically when the output needs to be human-readable, opened in a spreadsheet, or read by a non-NumPy tool
  • Set an appropriate fmt string explicitly when the default scientific-notation formatting isn't what you want for the output file
  • Use np.save() instead of savetxt() when exact precision and fast round-tripping matter more than readability or external tool compatibility

Interview Question

Why can saving a float array with np.savetxt() and then loading it back with np.loadtxt() sometimes produce values that aren't bit-for-bit identical to the originals?

Hint: Think about how savetxt()'s fmt parameter controls the text representation.

savetxt() converts each number into a text string using a specific format, like a fixed number of decimal places, controlled by the fmt parameter, and that conversion can truncate or round the value's actual binary precision if the format doesn't request enough digits to represent it exactly. Reading that rounded text representation back with loadtxt() reconstructs a float from the truncated decimal text, which can differ in its least significant bits from the original binary value that was saved, unlike np.save()'s binary .npy format, which stores the exact bits with no text conversion involved at all.

Exercises

MediumPractice using np.savetxt() in a real scenario.
View Solution
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")

with open("data.csv") as f:
    print(f.read())

Frequently Asked Questions

Why can saving a float array with np.savetxt() and then loading it back with np.loadtxt() sometimes produce values that aren't bit-for-bit identical to the originals?

savetxt() converts each number into a text string using a specific format, like a fixed number of decimal places, controlled by the fmt parameter, and that conversion can truncate or round the value's actual binary precision if the format doesn't request enough digits to represent it exactly. Reading that rounded text representation back with loadtxt() reconstructs a float from the truncated decimal text, which can differ in its least significant bits from the original binary value that was saved, unlike np.save()'s binary .npy format, which stores the exact bits with no text conversion involved at all.

Related Functions

np-loadtxtnp-savewith-statement