%%file inout.dat Here is a nice file with a couple lines of text it is a haiku f = open('inout.dat') print f.read() f.close() f = open('inout.dat') print f.readlines() f.close() for line in open('inout.dat'): print line.split() contents = open('inout.dat').read() out = open('my_output.dat', 'w') out.write(contents.replace(' ', '_')) out.close() !cat my_output.dat # writelines() is the opposite of readlines() lines = open('inout.dat').readlines() out = open('my_output.dat', 'w') out.writelines(lines) out.close() !cat my_output.dat %pylab inline import numpy as np import matplotlib.pyplot as plt arr = np.arange(10).reshape(2, 5) np.savetxt('test.out', arr, fmt='%.2e', header="My dataset") !cat test.out DataIn = np.loadtxt('test.out') print DataIn.shape print DataIn print DataIn[1,:] a, b, c, d, x = np.loadtxt('test.out', unpack=True) print a print b print c print d print x a, b = np.loadtxt('test.out',unpack=True, usecols=[0,1]) print a print b %%file input.csv # My csv example data 0.0, 1.1, 0.1 2.0, 1.9, 0.2 4.0, 3.2, 0.1 6.0, 4.0, 0.3 8.0, 5.9, 0.3 !cat input.csv #throws an error because commas are not part of floating point numbers x, y = np.loadtxt('input.csv',unpack=True, usecols=[0,1,2]) x, y = np.loadtxt('input.csv',unpack=True, delimiter=',', usecols=[0,1]) print x,y t = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) v = np.array([0.137,0.456,0.591,0.713,0.859,0.926,1.139,1.327,1.512,1.875]) p = 0.15 + v/10.0 np.savetxt('output.dat', (t,p)) DataOut = np.column_stack((t,p)) np.savetxt('output.dat', DataOut) !cat output.dat np.savetxt('output.dat', DataOut, fmt=('%3i', '%4.3f')) !cat output.dat myheader ="\nTime and Pressure data\nt (s) p (Pa)\n" np.savetxt('output.dat', DataOut, fmt=('%3i', '%4.3f'),header=myheader) !cat output.dat arr2 = DataIn #copy the array from before np.save('test.npy', arr2) # Now we read this back arr2n = np.load('test.npy') # Let's see if any element is non-zero in the difference. # A value of True would be a problem. print 'Any differences?', np.any(arr2-arr2n) np.savez('test.npz', arr, arr2) arrays = np.load('test.npz') arrays.files np.savez('test.npz', array1=arr, array2=arr2) arrays = np.load('test.npz') arrays.files print 'First row of first array:', arrays['array1'][0] # This is an equivalent way to get the same field print 'First row of first array:', arrays.f.array1[0]