Create some data¶
In [45]:
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 [46]:
data = np.column_stack([x,y_exp, y_1, y_2])
print(data)
[[ 0. -0.45117568 0. 2. ] [ 0.41666667 0.43189955 0.59138902 1.23729826] [ 0.83333333 0.71392141 0.89637813 -0.16205647] [ 1.25 0.74856219 1.14326263 -1.24786255] [ 1.66666667 1.61838799 1.35865518 -1.40680032] [ 2.08333333 1.66520902 1.5532997 -0.68433875] [ 2.5 1.33618026 1.73286211 0.34409962] [ 2.91666667 1.88656554 1.90078099 1.00503411] [ 3.33333333 1.77840862 2.05933617 0.95225291] [ 3.75 2.04066146 2.21013479 0.32747786] [ 4.16666667 2.17528515 2.35436208 -0.4008769 ] [ 4.58333333 2.49919192 2.49292301 -0.77320831] [ 5. 2.42237277 2.6265278 -0.61735433] [ 5.41666667 2.64153549 2.75574651 -0.10934473] [ 5.83333333 3.04934086 2.88104523 0.38728711] [ 6.25 2.60899685 3.00281108 0.57174799] [ 6.66666667 3.61930602 3.12136995 0.37959133] [ 7.08333333 3.26191351 3.23699932 -0.01430654] [ 7.5 3.49476329 3.34993791 -0.33901857] [ 7.91666667 3.21859606 3.46039293 -0.40735687] [ 8.33333333 3.1266568 3.56854561 -0.21704892] [ 8.75 3.43133318 3.67455567 0.0762659 ] [ 9.16666667 4.15563435 3.7785647 0.2780916 ] [ 9.58333333 4.00814021 3.88069898 0.27952457] [10. 3.92766452 3.98107171 0.1104558 ]]
Save to file¶
In [47]:
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.451, 0.000, 2.000
0.417, 0.432, 0.591, 1.237
0.833, 0.714, 0.896, -0.162
1.250, 0.749, 1.143, -1.248
1.667, 1.618, 1.359, -1.407
2.083, 1.665, 1.553, -0.684
2.500, 1.336, 1.733, 0.344
2.917, 1.887, 1.901, 1.005
3.333, 1.778, 2.059, 0.952
3.750, 2.041, 2.210, 0.327
4.167, 2.175, 2.354, -0.401
4.583, 2.499, 2.493, -0.773
5.000, 2.422, 2.627, -0.617
5.417, 2.642, 2.756, -0.109
5.833, 3.049, 2.881, 0.387
6.250, 2.609, 3.003, 0.572
6.667, 3.619, 3.121, 0.380
7.083, 3.262, 3.237, -0.014
7.500, 3.495, 3.350, -0.339
7.917, 3.219, 3.460, -0.407
8.333, 3.127, 3.569, -0.217
8.750, 3.431, 3.675, 0.076
9.167, 4.156, 3.779, 0.278
9.583, 4.008, 3.881, 0.280
10.000, 3.928, 3.981, 0.110
Load from file¶
In [48]:
loaded_data = np.loadtxt("data.dat", delimiter = ',') # delimiter needed if used above
print(loaded_data)
[[ 0. -0.451 0. 2. ] [ 0.417 0.432 0.591 1.237] [ 0.833 0.714 0.896 -0.162] [ 1.25 0.749 1.143 -1.248] [ 1.667 1.618 1.359 -1.407] [ 2.083 1.665 1.553 -0.684] [ 2.5 1.336 1.733 0.344] [ 2.917 1.887 1.901 1.005] [ 3.333 1.778 2.059 0.952] [ 3.75 2.041 2.21 0.327] [ 4.167 2.175 2.354 -0.401] [ 4.583 2.499 2.493 -0.773] [ 5. 2.422 2.627 -0.617] [ 5.417 2.642 2.756 -0.109] [ 5.833 3.049 2.881 0.387] [ 6.25 2.609 3.003 0.572] [ 6.667 3.619 3.121 0.38 ] [ 7.083 3.262 3.237 -0.014] [ 7.5 3.495 3.35 -0.339] [ 7.917 3.219 3.46 -0.407] [ 8.333 3.127 3.569 -0.217] [ 8.75 3.431 3.675 0.076] [ 9.167 4.156 3.779 0.278] [ 9.583 4.008 3.881 0.28 ] [10. 3.928 3.981 0.11 ]]
In [49]:
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.451, 0.000, 2.000
readlines function¶
In [50]:
ifile = open("data.dat", "r")
lines = ifile.readlines() # readlines function gets a list of all lines
for line in lines :
print(line, end='')
ifile.close()
# x, y_exp, y_1, y_2
0.000, -0.451, 0.000, 2.000
0.417, 0.432, 0.591, 1.237
0.833, 0.714, 0.896, -0.162
1.250, 0.749, 1.143, -1.248
1.667, 1.618, 1.359, -1.407
2.083, 1.665, 1.553, -0.684
2.500, 1.336, 1.733, 0.344
2.917, 1.887, 1.901, 1.005
3.333, 1.778, 2.059, 0.952
3.750, 2.041, 2.210, 0.327
4.167, 2.175, 2.354, -0.401
4.583, 2.499, 2.493, -0.773
5.000, 2.422, 2.627, -0.617
5.417, 2.642, 2.756, -0.109
5.833, 3.049, 2.881, 0.387
6.250, 2.609, 3.003, 0.572
6.667, 3.619, 3.121, 0.380
7.083, 3.262, 3.237, -0.014
7.500, 3.495, 3.350, -0.339
7.917, 3.219, 3.460, -0.407
8.333, 3.127, 3.569, -0.217
8.750, 3.431, 3.675, 0.076
9.167, 4.156, 3.779, 0.278
9.583, 4.008, 3.881, 0.280
10.000, 3.928, 3.981, 0.110
split function¶
In [51]:
words = line.split() # or line.split(',') will split on comma without including the comma
print(line)
print(words)
for w in words:
print(w)
10.000, 3.928, 3.981, 0.110 ['10.000,', '3.928,', '3.981,', '0.110'] 10.000, 3.928, 3.981, 0.110
Output¶
In [52]:
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 [53]:
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.
with statement¶
Above, we used open and close statements. But the Pythonic way to do this is with a with statement.
- Automatically closes files, even if errors happen inside the block (safer).
In [54]:
with open("data.dat", "r") as ifile:
lines = ifile.readlines()
for line in lines:
print(line, end='')
# x, y_exp, y_1, y_2
0.000, -0.451, 0.000, 2.000
0.417, 0.432, 0.591, 1.237
0.833, 0.714, 0.896, -0.162
1.250, 0.749, 1.143, -1.248
1.667, 1.618, 1.359, -1.407
2.083, 1.665, 1.553, -0.684
2.500, 1.336, 1.733, 0.344
2.917, 1.887, 1.901, 1.005
3.333, 1.778, 2.059, 0.952
3.750, 2.041, 2.210, 0.327
4.167, 2.175, 2.354, -0.401
4.583, 2.499, 2.493, -0.773
5.000, 2.422, 2.627, -0.617
5.417, 2.642, 2.756, -0.109
5.833, 3.049, 2.881, 0.387
6.250, 2.609, 3.003, 0.572
6.667, 3.619, 3.121, 0.380
7.083, 3.262, 3.237, -0.014
7.500, 3.495, 3.350, -0.339
7.917, 3.219, 3.460, -0.407
8.333, 3.127, 3.569, -0.217
8.750, 3.431, 3.675, 0.076
9.167, 4.156, 3.779, 0.278
9.583, 4.008, 3.881, 0.280
10.000, 3.928, 3.981, 0.110
In [55]:
with open("data.dat", "r") as ifile:
for line in ifile:
print(line.rstrip()) # strip removes leading and trailing whitespace, including newlines
# use lstrip or rstrip to just strip the left or right end
# x, y_exp, y_1, y_2
0.000, -0.451, 0.000, 2.000
0.417, 0.432, 0.591, 1.237
0.833, 0.714, 0.896, -0.162
1.250, 0.749, 1.143, -1.248
1.667, 1.618, 1.359, -1.407
2.083, 1.665, 1.553, -0.684
2.500, 1.336, 1.733, 0.344
2.917, 1.887, 1.901, 1.005
3.333, 1.778, 2.059, 0.952
3.750, 2.041, 2.210, 0.327
4.167, 2.175, 2.354, -0.401
4.583, 2.499, 2.493, -0.773
5.000, 2.422, 2.627, -0.617
5.417, 2.642, 2.756, -0.109
5.833, 3.049, 2.881, 0.387
6.250, 2.609, 3.003, 0.572
6.667, 3.619, 3.121, 0.380
7.083, 3.262, 3.237, -0.014
7.500, 3.495, 3.350, -0.339
7.917, 3.219, 3.460, -0.407
8.333, 3.127, 3.569, -0.217
8.750, 3.431, 3.675, 0.076
9.167, 4.156, 3.779, 0.278
9.583, 4.008, 3.881, 0.280
10.000, 3.928, 3.981, 0.110
Read and store in an array¶
Option 1
In [56]:
with open("data.dat", "r") as ifile:
lines = ifile.readlines()
nrows = len(lines)-1 # rows of data (don't include the header)
ncols = len(lines[1].split()) # columns of data; lines[1] is the first line of data
data = np.empty((nrows, ncols))
for i, line in enumerate(lines[1:]): # skip the header line
row = line.split(',') # split the line into numbers without the comma
for j in range(len(row)):
row[j] = float(row[j])
data[i,:] = row
data
Out[56]:
array([[ 0. , -0.451, 0. , 2. ],
[ 0.417, 0.432, 0.591, 1.237],
[ 0.833, 0.714, 0.896, -0.162],
[ 1.25 , 0.749, 1.143, -1.248],
[ 1.667, 1.618, 1.359, -1.407],
[ 2.083, 1.665, 1.553, -0.684],
[ 2.5 , 1.336, 1.733, 0.344],
[ 2.917, 1.887, 1.901, 1.005],
[ 3.333, 1.778, 2.059, 0.952],
[ 3.75 , 2.041, 2.21 , 0.327],
[ 4.167, 2.175, 2.354, -0.401],
[ 4.583, 2.499, 2.493, -0.773],
[ 5. , 2.422, 2.627, -0.617],
[ 5.417, 2.642, 2.756, -0.109],
[ 5.833, 3.049, 2.881, 0.387],
[ 6.25 , 2.609, 3.003, 0.572],
[ 6.667, 3.619, 3.121, 0.38 ],
[ 7.083, 3.262, 3.237, -0.014],
[ 7.5 , 3.495, 3.35 , -0.339],
[ 7.917, 3.219, 3.46 , -0.407],
[ 8.333, 3.127, 3.569, -0.217],
[ 8.75 , 3.431, 3.675, 0.076],
[ 9.167, 4.156, 3.779, 0.278],
[ 9.583, 4.008, 3.881, 0.28 ],
[10. , 3.928, 3.981, 0.11 ]])
Option 2
- Use a list comprehension (Google: "Python list comprehension")
In [57]:
with open("data.dat", "r") as ifile:
lines = ifile.readlines()
nrows = len(lines)-1 # rows of data (don't include the header)
ncols = len(lines[1].split()) # columns of data; lines[1] is the first line of data
data = np.empty((nrows, ncols))
for i, line in enumerate(lines[1:]):
data[i,:] = np.array( [float(v) for v in line.split(',') ]) # list comprehention
data
Out[57]:
array([[ 0. , -0.451, 0. , 2. ],
[ 0.417, 0.432, 0.591, 1.237],
[ 0.833, 0.714, 0.896, -0.162],
[ 1.25 , 0.749, 1.143, -1.248],
[ 1.667, 1.618, 1.359, -1.407],
[ 2.083, 1.665, 1.553, -0.684],
[ 2.5 , 1.336, 1.733, 0.344],
[ 2.917, 1.887, 1.901, 1.005],
[ 3.333, 1.778, 2.059, 0.952],
[ 3.75 , 2.041, 2.21 , 0.327],
[ 4.167, 2.175, 2.354, -0.401],
[ 4.583, 2.499, 2.493, -0.773],
[ 5. , 2.422, 2.627, -0.617],
[ 5.417, 2.642, 2.756, -0.109],
[ 5.833, 3.049, 2.881, 0.387],
[ 6.25 , 2.609, 3.003, 0.572],
[ 6.667, 3.619, 3.121, 0.38 ],
[ 7.083, 3.262, 3.237, -0.014],
[ 7.5 , 3.495, 3.35 , -0.339],
[ 7.917, 3.219, 3.46 , -0.407],
[ 8.333, 3.127, 3.569, -0.217],
[ 8.75 , 3.431, 3.675, 0.076],
[ 9.167, 4.156, 3.779, 0.278],
[ 9.583, 4.008, 3.881, 0.28 ],
[10. , 3.928, 3.981, 0.11 ]])
Option 3
- Use a nested list comprehension (Google: "Python list comprehension")
- Note, we don't need to figure out the number of rows and columns, it's built in
In [58]:
with open("data.dat", "r") as ifile:
lines = ifile.readlines()
data = np.array([ [float(v) for v in line.split(',')] for line in lines[1:] ])
data
Out[58]:
array([[ 0. , -0.451, 0. , 2. ],
[ 0.417, 0.432, 0.591, 1.237],
[ 0.833, 0.714, 0.896, -0.162],
[ 1.25 , 0.749, 1.143, -1.248],
[ 1.667, 1.618, 1.359, -1.407],
[ 2.083, 1.665, 1.553, -0.684],
[ 2.5 , 1.336, 1.733, 0.344],
[ 2.917, 1.887, 1.901, 1.005],
[ 3.333, 1.778, 2.059, 0.952],
[ 3.75 , 2.041, 2.21 , 0.327],
[ 4.167, 2.175, 2.354, -0.401],
[ 4.583, 2.499, 2.493, -0.773],
[ 5. , 2.422, 2.627, -0.617],
[ 5.417, 2.642, 2.756, -0.109],
[ 5.833, 3.049, 2.881, 0.387],
[ 6.25 , 2.609, 3.003, 0.572],
[ 6.667, 3.619, 3.121, 0.38 ],
[ 7.083, 3.262, 3.237, -0.014],
[ 7.5 , 3.495, 3.35 , -0.339],
[ 7.917, 3.219, 3.46 , -0.407],
[ 8.333, 3.127, 3.569, -0.217],
[ 8.75 , 3.431, 3.675, 0.076],
[ 9.167, 4.156, 3.779, 0.278],
[ 9.583, 4.008, 3.881, 0.28 ],
[10. , 3.928, 3.981, 0.11 ]])
In [ ]: