I am trying to write data in a file within several loops. The difficulty I have is that data are changing at the s ame time in lines and in columns. What I am getting is:
________________________________________________________
f=25,0 | b=-25,0 | a=-30 |
________________________________________________________
d | Ka | Kah | th | Ko | 75/25| 50/50| Egb |
________________________________________________________
-25,0 | 0,76 | 0,44 | 60 | 0,40 | 0,49| 0,58| 1,43|
-17,0 | 0,37 | 0,25 | 46 | 0,40 | 0,39| 0,39| 2,61|
-13,0 | 0,32 | 0,23 | 45 | 0,40 | 0,38| 0,36| 2,75|
-8,0 | 0,27 | 0,22 | 44 | 0,40 | 0,37| 0,34| 2,90|
0,0 | 0,23 | 0,20 | 43 | 0,40 | 0,36| 0,32| 3,08|
8,0 | 0,20 | 0,19 | 42 | 0,40 | 0,35| 0,30| 3,27|
13,0 | 0,19 | 0,18 | 41 | 0,40 | 0,35| 0,29| 3,49|
17,0 | 0,18 | 0,18 | 41 | 0,40 | 0,35| 0,29| 3,49|
25,0 | 0,17 | 0,17 | 40 | 0,40 | 0,34| 0,29| 3,73|
________________________________________________________
________________________________________________________
f=25,0 | b=-25,0 | a=-20 |
________________________________________________________
d | Ka | Kah | th | Ko | 75/25| 50/50| Egb |
________________________________________________________
-25,0 | 0,80 | 0,57 | 70 | 0,45 | 0,54| 0,63| 1,00|
-17,0 | 0,43 | 0,35 | 53 | 0,45 | 0,45| 0,44| 1,88|
-13,0 | 0,38 | 0,32 | 51 | 0,45 | 0,43| 0,42| 2,05|
-8,0 | 0,33 | 0,29 | 49 | 0,45 | 0,42| 0,39| 2,25|
0,0 | 0,29 | 0,27 | 48 | 0,45 | 0,41| 0,37| 2,36|
8,0 | 0,26 | 0,25 | 46 | 0,45 | 0,40| 0,35| 2,61|
13,0 | 0,24 | 0,24 | 46 | 0,45 | 0,40| 0,35| 2,61|
17,0 | 0,24 | 0,24 | 45 | 0,45 | 0,40| 0,34| 2,75|
25,0 | 0,23 | 0,22 | 44 | 0,45 | 0,40| 0,34| 2,90|
________________________________________________________
What I would like to get is:
_______________________________________________________________________________________________________________________________
f=25,0 | b=-25,0
_______________________________________________________________________________________________________________________________
a=-30 | a=-20
_______________________________________________________________________________________________________________________________
d | Ka | Kah | th | Ko | 75/25 | 50/50 | Egb | Ka | Kah | th | Ko | 75/25 | 50/50 | Egb |
_______________________________________________________________________________________________________________________________
-25,0 | 0,76 | 0,44 | 60 | 0,40 | 0,49 | 0,58 | 1,43 | 0,80 | 0,57 | 70 | 0,45| 0,54 | 0,63 | 1,00 |
-17,0 | 0,37 | 0,25 | 46 | 0,40 | 0,39 | 0,39 | 2,61 | 0,43 | 0,35 | 53 | 0,45| 0,45 | 0,44 | 1,88 |
-13,0 | 0,32 | 0,23 | 45 | 0,40 | 0,38 | 0,36 | 2,75 | 0,38 | 0,32 | 51 | 0,45| 0,43 | 0,42 | 2,05 |
-8,0 | 0,27 | 0,22 | 44 | 0,40 | 0,37 | 0,34 | 2,90 | 0,33 | 0,29 | 49 | 0,45| 0,42 | 0,39 | 2,25 |
0,0 | 0,23 | 0,20 | 43 | 0,40 | 0,36 | 0,32 | 3,08 | 0,29 | 0,27 | 48 | 0,45| 0,41 | 0,37 | 2,36 |
8,0 | 0,20 | 0,19 | 42 | 0,40 | 0,35 | 0,30 | 3,27 | 0,26 | 0,25 | 46 | 0,45| 0,40 | 0,35 | 2,61 |
13,0 | 0,19 | 0,18 | 41 | 0,40 | 0,35 | 0,29 | 3,49 | 0,24 | 0,24 | 46 | 0,45| 0,40 | 0,35 | 2,61 |
17,0 | 0,18 | 0,18 | 41 | 0,40 | 0,35 | 0,29 | 3,49 | 0,24 | 0,24 | 45 | 0,45| 0,40 | 0,34 | 2,75 |
25,0 | 0,17 | 0,17 | 40 | 0,40 | 0,34 | 0,29 | 3,73 | 0,23 | 0,22 | 44 | 0,45| 0,40 | 0,34 | 2,90 |
_______________________________________________________________________________________________________________________________
And here is my code:
import numpy as np
import math as m
import csv
file = open("test.txt", 'w')
phi_list=[25.]
alfa_list=[-30,-20,-10,0,10,20,30]
b=0
#
for f in phi_list:
i = int(2*f/5+1)
print 'i =',i
bi = np.arange(-f,f+5,5)
beta = bi.tolist()
print "beta:", beta
d = np.arange(-f,f+5,f/3)
di = d.tolist()
print "di:", di
di.insert(1,-f/2)
print "di:", di
di.insert(5,f/2)
dk = sorted(di)
delta = [round(elem, 0) for elem in dk]
print "delta:", delta
for bo in beta:
# file.write("\n" + "\n" +'f=' +'%s'%f+ ' | ' +'b=' +'%s'%b + "\n" )
if bo == f and bo > 0:
b = bo-0.01
elif b == f and bo < 0:
b = bo+0.01
for a in alfa_list:
file.write("\n" +"\n" +'____________________________________________________________________________________' +"\n" )
file.write( ' f=' +'%s'%f + ' | ' +' b=' +'%s'%(bo) + ' | ' +'a=' +'%s'%a + ' | ' +"\n" )
file.write('____________________________________________________________________________________' +"\n" )
file.write(' d ' + ' | ' + ' Ka '+ ' | ' + ' Kah'+ ' | ' + ' th '+ ' | '+ ' Ko '+ ' |'+ ' 75/25'+ '|'+
' 50/50'+ '| '+ ' Egb ' + '| '+ "\n")
file.write('____________________________________________________________________________________' +"\n" )
for d in delta:
Ka=(csfa**2)/((csa**2)/(csad*csba)))**2)
#
theta=round(f+m.degrees(m.atan(csfa/(snfa+m.sqrt((snfd*csab/(snfb*csad)))))))
#
fo=1-abs(tna*tnb)
print "fo:", fo
K1=(snf)/(snf-snb*2)*csb*2
print "K1:", K1
tna1=m.sqrt(1/((1/K1)))
print "Tana1:", tna1
Ko=K1*fo*(1+tna1)/(1+tna1*tndo)
Ko5=0.5*Ko+0.5*Ka
Ko3=0.75*Ko+0.25*Ka
Kah=Ka*csad
Edb=1/(m.tan(m.radians(theta-f)))
file.write( '{0:5.1f} {1:s} {2:.2f} {3:s} {4:.2f} {5:s} {6:4.0f} {7:s} {8:.2f} {9:s} {10:5.2f}{11:s} {12:5.2f}{13:s} {14:5.2f}{15:s} \n'.format(d,
'|', Ka,'|', Kah, '|', theta, '|', Ko, '|', Ko3, '|', Ko5,'|', Edb,'|'))
file.write('____________________________________________________________________________________' +"\n" )
file.close()
What I need is the data for the new alpha to be added in lines and not to next columns. I have tried with join but without success. Any suggestions or solutions?
You're looping over a
, and in these loops, you're writing to the file with newlines, and do all computation for first a
before computing the next one. So, no surprise, you write the blocks one after another.
What you should do would be to invert loops over alfa_list
and delta
: in fact, d
is really your row axis, and for each value of them, write all figures for all a
values.
Tip: when looping over alfa_list
, add newline character only for the last one of the list. You can follow that using for a_idx, a in enumerate(alfa_list):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.