Basic File I/O

Numpy

  • np.savetxt
  • np.loadtxt
  • np.column_stack

Create some data

In [1]:
import numpy as np

x     = np.linspace(0,10,25)
y_exp = x**0.6 + (np.random.rand(25) -0.5)        # ploting data series below
y_1   = x**0.6
y_2   = np.exp(-0.2*x)*np.cos(2*x)*2

Create a single 2-D array (matrix) for saving to a file

In [2]:
data = np.column_stack([x,y_exp, y_1, y_2])
print(data)
[[ 0.         -0.35717645  0.          2.        ]
 [ 0.41666667  0.94742279  0.59138902  1.23729826]
 [ 0.83333333  0.39952824  0.89637813 -0.16205647]
 [ 1.25        1.54093399  1.14326263 -1.24786255]
 [ 1.66666667  1.43347207  1.35865518 -1.40680032]
 [ 2.08333333  1.61566022  1.5532997  -0.68433875]
 [ 2.5         1.59673552  1.73286211  0.34409962]
 [ 2.91666667  1.98907917  1.90078099  1.00503411]
 [ 3.33333333  2.53397802  2.05933617  0.95225291]
 [ 3.75        2.25914272  2.21013479  0.32747786]
 [ 4.16666667  2.32622551  2.35436208 -0.4008769 ]
 [ 4.58333333  2.10955187  2.49292301 -0.77320831]
 [ 5.          2.23922853  2.6265278  -0.61735433]
 [ 5.41666667  2.31294833  2.75574651 -0.10934473]
 [ 5.83333333  3.1431754   2.88104523  0.38728711]
 [ 6.25        3.01084514  3.00281108  0.57174799]
 [ 6.66666667  3.21029313  3.12136995  0.37959133]
 [ 7.08333333  3.47449335  3.23699932 -0.01430654]
 [ 7.5         3.51601487  3.34993791 -0.33901857]
 [ 7.91666667  2.97033186  3.46039293 -0.40735687]
 [ 8.33333333  3.94996781  3.56854561 -0.21704892]
 [ 8.75        3.95343275  3.67455567  0.0762659 ]
 [ 9.16666667  3.52136441  3.7785647   0.2780916 ]
 [ 9.58333333  3.42315875  3.88069898  0.27952457]
 [10.          3.85884597  3.98107171  0.1104558 ]]

Save to file

In [3]:
np.savetxt("data.dat", data)

#----- OR THIS

np.savetxt("data.dat", data, fmt='%10.3f', 
           header="x, y_exp, y_1, y_2",
           delimiter = ',')

!cat data.dat
# x, y_exp, y_1, y_2
     0.000,    -0.357,     0.000,     2.000
     0.417,     0.947,     0.591,     1.237
     0.833,     0.400,     0.896,    -0.162
     1.250,     1.541,     1.143,    -1.248
     1.667,     1.433,     1.359,    -1.407
     2.083,     1.616,     1.553,    -0.684
     2.500,     1.597,     1.733,     0.344
     2.917,     1.989,     1.901,     1.005
     3.333,     2.534,     2.059,     0.952
     3.750,     2.259,     2.210,     0.327
     4.167,     2.326,     2.354,    -0.401
     4.583,     2.110,     2.493,    -0.773
     5.000,     2.239,     2.627,    -0.617
     5.417,     2.313,     2.756,    -0.109
     5.833,     3.143,     2.881,     0.387
     6.250,     3.011,     3.003,     0.572
     6.667,     3.210,     3.121,     0.380
     7.083,     3.474,     3.237,    -0.014
     7.500,     3.516,     3.350,    -0.339
     7.917,     2.970,     3.460,    -0.407
     8.333,     3.950,     3.569,    -0.217
     8.750,     3.953,     3.675,     0.076
     9.167,     3.521,     3.779,     0.278
     9.583,     3.423,     3.881,     0.280
    10.000,     3.859,     3.981,     0.110

Load from file

In [4]:
loaded_data = np.loadtxt("data.dat", delimiter = ',')    # delimiter needed if used above
print(loaded_data)
[[ 0.    -0.357  0.     2.   ]
 [ 0.417  0.947  0.591  1.237]
 [ 0.833  0.4    0.896 -0.162]
 [ 1.25   1.541  1.143 -1.248]
 [ 1.667  1.433  1.359 -1.407]
 [ 2.083  1.616  1.553 -0.684]
 [ 2.5    1.597  1.733  0.344]
 [ 2.917  1.989  1.901  1.005]
 [ 3.333  2.534  2.059  0.952]
 [ 3.75   2.259  2.21   0.327]
 [ 4.167  2.326  2.354 -0.401]
 [ 4.583  2.11   2.493 -0.773]
 [ 5.     2.239  2.627 -0.617]
 [ 5.417  2.313  2.756 -0.109]
 [ 5.833  3.143  2.881  0.387]
 [ 6.25   3.011  3.003  0.572]
 [ 6.667  3.21   3.121  0.38 ]
 [ 7.083  3.474  3.237 -0.014]
 [ 7.5    3.516  3.35  -0.339]
 [ 7.917  2.97   3.46  -0.407]
 [ 8.333  3.95   3.569 -0.217]
 [ 8.75   3.953  3.675  0.076]
 [ 9.167  3.521  3.779  0.278]
 [ 9.583  3.423  3.881  0.28 ]
 [10.     3.859  3.981  0.11 ]]

General File IO

Input

readline function

In [5]:
ifile = open("data.dat", "r")   # open a file for reading
s = ifile.readline()
print(s)
s = ifile.readline()            # readline function will read the next line in the file
print(s)
ifile.close()                   # close the file when you're done
# x, y_exp, y_1, y_2

     0.000,    -0.357,     0.000,     2.000

readlines function

In [6]:
ifile = open("data.dat", "r")
lines = ifile.readlines()             # readlines function gets a list of all lines
for line in lines :
    print(line, end='')
# x, y_exp, y_1, y_2
     0.000,    -0.357,     0.000,     2.000
     0.417,     0.947,     0.591,     1.237
     0.833,     0.400,     0.896,    -0.162
     1.250,     1.541,     1.143,    -1.248
     1.667,     1.433,     1.359,    -1.407
     2.083,     1.616,     1.553,    -0.684
     2.500,     1.597,     1.733,     0.344
     2.917,     1.989,     1.901,     1.005
     3.333,     2.534,     2.059,     0.952
     3.750,     2.259,     2.210,     0.327
     4.167,     2.326,     2.354,    -0.401
     4.583,     2.110,     2.493,    -0.773
     5.000,     2.239,     2.627,    -0.617
     5.417,     2.313,     2.756,    -0.109
     5.833,     3.143,     2.881,     0.387
     6.250,     3.011,     3.003,     0.572
     6.667,     3.210,     3.121,     0.380
     7.083,     3.474,     3.237,    -0.014
     7.500,     3.516,     3.350,    -0.339
     7.917,     2.970,     3.460,    -0.407
     8.333,     3.950,     3.569,    -0.217
     8.750,     3.953,     3.675,     0.076
     9.167,     3.521,     3.779,     0.278
     9.583,     3.423,     3.881,     0.280
    10.000,     3.859,     3.981,     0.110

split function

In [7]:
words = line.split()
print(line)
print(words)
for w in words:
    print(w)
    10.000,     3.859,     3.981,     0.110

['10.000,', '3.859,', '3.981,', '0.110']
10.000,
3.859,
3.981,
0.110

Output

In [10]:
ofile = open("output.dat", "w")         # Open a file for writing: ofile can be any name
ofile.write("Some header\n")            # write some text: note the "\n" makes a new line
ofile.write("some new line.")           # write another line of text (note there is no \n)
ofile.write(" This isnt a new line\n")  # and another
ofile.write("The third line")           # and other
ofile.close()                           # close the file

!cat output.dat
Some header
some new line. This isnt a new line
The third line

Triple strings preserve white space

In [14]:
ofile = open("triplestring.dat", "w")
s = """Hi there,
How is your day going so far?

    I hope you are good."""

ofile.write(s)
ofile.close()
!cat triplestring.dat
Hi there,
How is your day going so far?

    I hope you are good.
In [ ]: