1.

I Want To Load Data From A Text File. How Do I Make This Code More Efficient?

Answer»

Use numpy.loadtxt(). Even if your text file has header and footer lines or comments, loadtxt can almost certainly read it; it is convenient and efficient.

If you FIND this still too slow, you should consider changing to a more efficient file format than plain text. There are a large number of alternatives, depending on your needs (and on which version of NumPy/SciPy you are using):

  • Text files: slow, huge, portable, human-readable; built into NumPy
  • Raw binary: no metadata, totally unportable, FAST; built into NumPy
  • pickle: somewhat slow, somewhat portable (may be incompatible with different NumPy VERSIONS); built into NumPy
  • MATLAB format: portable; built into SciPy (scipy.io.loadmat())
  • HDF5: high-powered kitchen-sink format; both PyTables and h5py provide a NumPy friendly interface on top of the core HDF5 library written in C.
  • FITS: standard kitchen-sink format in astronomy; the astropy library provides a convenient Python interface through its io.fits PACKAGE.
  • .npy: NumPy native binary data format, simple, efficient, portable; built into NumPy as of 1.0.5.

Use numpy.loadtxt(). Even if your text file has header and footer lines or comments, loadtxt can almost certainly read it; it is convenient and efficient.

If you find this still too slow, you should consider changing to a more efficient file format than plain text. There are a large number of alternatives, depending on your needs (and on which version of NumPy/SciPy you are using):



Discussion

No Comment Found