请问如何整齐地输出数据?
请问Python中如何整齐地输出数据?
while i < len(state_1):
x_displacement = state_1.direction_x.displacement
y_displacement = state_1.direction_y.displacement
z_displacement = state_1.direction_z.displacement
x_force = state_1.direction_x.force
y_force = state_1.direction_y.force
z_force = state_1.direction_z.force
x_initial_speed = state_1.direction_x.initial_speed
y_initial_speed = state_1.direction_y.initial_speed
z_initial_speed = state_1.direction_z.initial_speed
x_timestep = state_1.direction_x.time_step
y_timestep = state_1.direction_y.time_step
z_timestep = state_1.direction_z.time_step
x_TF = state_1.direction_x.TF
y_TF = state_1.direction_y.TF
z_TF = state_1.direction_z.TF
x_mass = state_1.direction_x.mass
y_mass = state_1.direction_y.mass
z_mass = state_1.direction_z.mass
#m_msg(str(x_displacement) + " " + str(y_displacement)+ " " + str(z_displacement)+ " | " + str(x_force) + " " + str(y_force) + " " + str(z_force) + " | " + str(x_initial_speed) + " " + str(y_initial_speed) + " " + str(z_initial_speed) + " | " + str(x_TF) + " " + str(y_TF) + " " + str(z_TF) + " | " + str(x_timestep) + " " + str(y_timestep) + " " + str(z_timestep)+ " | " + str(x_mass) + " " + str(y_mass) + " " + str(z_mass) )
f.write(str(x_displacement) + "," + str(y_displacement)+ "," + str(z_displacement)+ "," + str(x_force) + "," + str(y_force) + "," + str(z_force) + "," + str(x_TF) + "," + str(y_TF) + "," + str(z_TF) + "\n" ) #changed
i = i + 1
f.flush()
需要整齐地输出这些数据str(x_displacement) + "," + str(y_displacement)+ "," + str(z_displacement)+ "," + str(x_force) + "," + str(y_force) + "," + str(z_force) + "," + str(x_TF) + "," + str(y_TF) + "," + str(z_TF) 返回小木虫查看更多
编写一个小函数,定义成
def Myform(str, *value)
第一个变量用来控制输出类型,第二个变量自动搜集所有数据,之后在自定义函数里利用format函数获得想要的结果
谢谢!上边的代码只是一部分,怎样做到自动搜集所有数据?
括号里面的第二个变量就是用来搜集数据的,只要是单一数据而不是列表,都会默认放到这个数据里,调用方法如 def(filename, a,b, c,……)
def Myformat(mf,*mn):
ii=len(mn)
with open(mf,'a') as f:
for i in range(ii):
f.write(str('{:.7e}'.format(mn))+'\t')
f.write('\n')
想输出7位小数的科学计数法如上例所示,可以根据个人爱好改编
,