Posted on

A trick to use numpy.loadtxt on strings

numpy offers a function, numpy.loadtxt, to read in an array from a file. This is a extremely useful function especially when a 2d array is being read. However the function only accepts either a file path/name, or a file generator (file handler). You can not use a string directly as the argument.

numpy also offers a function numpy.fromstring to convert a string to an array, but the functionality is much more limited compared to numpy.loadtxt. Taking a simple example, if your string looks like

0.0 1.0e+02 2.0
3.0 4.0e-02 5.0

numpy.fromstring won’t do you any good. To solve this problem, you may use the io module.


The old StringIO module in Python 2 is now integrated into the new io module. The full documentation of io can be found here:
io module

io.StringIO creates a file stream, acceptable by the numpy.loadtxt now. So, supposedly your string above is saved as a variable var, all you need to do is,

import io
import numpy as np
f_handler = io.StringIO(var)
newarray = np.loadtxt(f_handler)

Notice that the stream has to be closed after use.