Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write data to a file in Python

Tags:

python

How can I save data to a file (that I will plot later on) effectively?

I have got this from my research:

#Open new data file
f = open("data2.txt", "w")
f.write( str(yEst)  )      # str() converts to string
f.close()

Here is a bit of my code so far:

for i in drange2(0, 2*math.pi + 0.0634665182543392 , 0.0634665182543392):
    for x in range(1,N+1):
       yEst = yEst + a * cos(x* i)
#print yEst
f.write( str(yEst)  )      # str() converts to string
yEst=0
f.close()

Now, when I go and open my file "data2.txt" I can not read the data because it is not 'organized'. How can I go to the next line using the f.write( str(yEst) ) so that I have a column that contains my 'yEst' data into the file "data2.txt? Thank you in advance for your consideration :)

PS: yEst looks like (in the data2.txt file): 48.901347147148.605785828748.114506165947.429486 .. and I want it as a column: -->

48.9013471471 (new line)
48.6057858287 (new line)
48.1145061659 (new line)
47.4294863684    
etc ..                        
like image 774
Faycal F Avatar asked Aug 28 '12 18:08

Faycal F


3 Answers

Do you mean to say you'd like each point of data on its own line, like this?

48.9013471471
48.6057858287
48.1145061659
47.4294863684

If so, then you might like to try something like this:

for i in drange2(0, 2*math.pi + 0.0634665182543392 , 0.0634665182543392):
    for x in range(1,N+1):
       yEst = yEst + a * cos(x* i) 
       f.write( str(yEst) + "\n"  )
f.close()

Firstly, to write each data point to the line, it needs to be 'inside' the loop - that's why I've added extra space to the line before I call f.write.

The second thing I added is + "\n" - this will add the new line character to the end of the line. You can change it to whatever you'd like! A couple of examples:

f.write( str(yEst) + " " ) will add one space after each data point:

48.9013471471 48.6057858287 48.1145061659 47.4294863684

f.write( str(yEst) + "|" ) will add one pipe character after each data point:

48.9013471471|48.6057858287|48.1145061659|47.4294863684|

If, on the other hand, you'd rather just save the data as an array, try the below:

yEst = 0 # or some initial value
yEstArray = []
for i in drange2(0, 2*math.pi + 0.0634665182543392 , 0.0634665182543392):
    for x in range(1,N+1):
       yEst = yEst + a * cos(x* i) 
       yEstArray.append(yEst)

Then, you can iterate over the array like so:

for yEst in yEstArray:
    do something with yEst

or

for yEst, index in enumerate(yEstArray):
    do something with yEst and its index
like image 104
Hannele Avatar answered Oct 24 '22 08:10

Hannele


You'll want pickle for data serialization.

like image 41
Dhaivat Pandya Avatar answered Oct 24 '22 07:10

Dhaivat Pandya


Python has three main built-in ways to persist data:

  1. pickle which serialises objects to files;
  2. sqlite - an embedded SQL database, which is supported by many ORM systems (which with that addition is an excellent alternative to pickle for object storage); and
  3. json/yaml - serialisation intended for the web, with basic datastructures and types.
like image 6
Marcin Avatar answered Oct 24 '22 08:10

Marcin