[英]Python write lines in file with loop
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. 我需要的是将新Alpha数据添加到行中而不是添加到下一列中的数据。 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. 您正在遍历
a
,在这些循环中,您正在用换行符写入文件,并在计算下一个之前对前a
进行所有计算。 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. 你应该做的是反转了循环
alfa_list
和delta
:其实, d
是真的是你的行轴,并为他们的每个值,写入所有所有的数字a
值。
Tip: when looping over alfa_list
, add newline character only for the last one of the list. 提示:遍历
alfa_list
,仅为列表的最后一个添加换行符。 You can follow that using for a_idx, a in enumerate(alfa_list):
您可以将其
for a_idx, a in enumerate(alfa_list):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.