Reading numerical data from text filesΒΆ

We’ve been reading values from text files in the exercises.

Here is some revision on how to do that, going from the crude to the elegant way.

I first write a little text file out to disk:

numbers = [1.2, 2.3, 3.4, 4.5]
fobj = open('some_numbers.txt', 'wt')
for number in numbers:
    # String version of number, plus end-of-line character
    fobj.write(str(number) + '\n')

Now I read it back again. First, I will just read the strings:

fobj = open('some_numbers.txt', 'rt')
lines = fobj.readlines()

Next I will read the file, but converting each number to a float:

fobj = open('some_numbers.txt', 'rt')
numbers_again = []
for line in fobj.readlines():

This gives:

[1.2, 2.3, 3.4, 4.5]

In fact we can do this even more simply by using np.loadtxt:

import numpy as np

The output is:

array([ 1.2,  2.3,  3.4,  4.5])