Register to reply 
How to write a data file in Python 
Share this thread: 
#1
Nov1810, 04:54 PM

P: 19

Hi guys
I've done a RungeKutta script for the Lorenz Equations in python, I need to write data for (t,x,y,z) in a .dat file in order to reprint in a table. How can a do it? Imported routines are: import sys, pylab, numpy from pylab import * from mpl_toolkits.mplot3d import Axes3D from numpy import * import matplotlib.pyplot as plt Mi iteration code is: for n in xrange(0,N): # at each time step calculate new x(t),y(t),z(t) # and append to lists x0, y0, z0 x,y,z = RKTwoD(x0[n],y0[n],z0[n],VDPXDot,VDPYDot,VDPZDot,dt) x0.append(x) y0.append(y) z0.append(z) t.append(t[n] + dt) savetxt('output.dat', (t,x0,y0,z0)) But it doesn't work, file just contain six columns instead of four and it takes too long to execute the script. Please can anyone help me? Thank you 


#2
Nov3010, 01:15 AM

P: 670

If you keep everything as an array instead of a list, basically take
x0.append(x) y0.append(y) z0.append(z) and do a vstack or hstack at the end so table = hstack(x0,y0,z0) then you just need to write out table.tofile('output.dat') numpy lists don't write out straight to dats 


#3
Nov3010, 03:19 PM

P: 19

Thanks for your answer but it doesn't work
I'm trying



#4
Nov3010, 03:25 PM

Mentor
P: 21,216

How to write a data file in Python
My understanding is that Python is an interpreted language, so if you have a loop that runs for many iterations, it's going to take a long time. The way around that is to use a language that is compiled, such as C or C++.
I don't know enough about Python to say anything intelligible about your file I/O problem... 


#5
Nov3010, 04:59 PM

P: 19

Thanks for your answer Mark44
No matter if the script takes too much time (I'm doing the script also for C++ and Java), but there must be some way to save all data in a unique file. By splitting it in four I have to use Ultraedit and it's a bit tiring.. 


#6
Nov3010, 05:34 PM

P: 415

look into numpy.savetxt(...)



#7
Dec110, 08:08 PM

P: 670

table = hstack([x0,y0,z0,t]) but when you open it, you need to specify the size of the array and make sure it's saved and unpacked as the right type. you can also look into scipy.io.savemat



#8
Dec810, 03:46 PM

P: 19

Thank you 


#9
Dec810, 04:26 PM

P: 670




#10
Dec910, 04:32 AM

P: 19




Register to reply 
Related Discussions  
Fortran 95: WRITE to screen changes output to WRITE to file  Programming & Computer Science  13  
Write an exponential equation from this data (data table included)  Precalculus Mathematics Homework  2  
Write an is_prime function in Python  Engineering, Comp Sci, & Technology Homework  1  
Loading file in python shell  Programming & Computer Science  1  
Loading a file (module) in Python  Computing & Technology  6 