24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1447  |  回复: 4

zhaoshf901

金虫 (著名写手)

[求助] 请问如何整齐地输出数据?已有1人参与

请问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)
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Gemener1

金虫 (初入文坛)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
感谢参与,应助指数 +1
zhaoshf901: 金币+60, 有帮助 2018-09-04 21:25:18
jjdg: 金币+1, 感谢参与 2018-09-05 15:06:15
编写一个小函数,定义成
def Myform(str, *value)
第一个变量用来控制输出类型,第二个变量自动搜集所有数据,之后在自定义函数里利用format函数获得想要的结果
与优秀的人做朋友
2楼2018-09-04 20:34:46
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhaoshf901

金虫 (著名写手)

引用回帖:
2楼: Originally posted by Gemener1 at 2018-09-04 20:34:46
编写一个小函数,定义成
def Myform(str, *value)
第一个变量用来控制输出类型,第二个变量自动搜集所有数据,之后在自定义函数里利用format函数获得想要的结果

谢谢!上边的代码只是一部分,怎样做到自动搜集所有数据?
3楼2018-09-04 21:24:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Gemener1

金虫 (初入文坛)


jjdg: 金币+1, 感谢参与 2018-09-06 08:56:39
引用回帖:
3楼: Originally posted by zhaoshf901 at 2018-09-04 21:24:21
谢谢!上边的代码只是一部分,怎样做到自动搜集所有数据?...

括号里面的第二个变量就是用来搜集数据的,只要是单一数据而不是列表,都会默认放到这个数据里,调用方法如 def(filename, a,b, c,……)
与优秀的人做朋友
4楼2018-09-05 19:06:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

Gemener1

金虫 (初入文坛)

★ ★
jjdg: 金币+2, 感谢参与 2018-09-06 08:56:46
引用回帖:
3楼: Originally posted by zhaoshf901 at 2018-09-04 21:24:21
谢谢!上边的代码只是一部分,怎样做到自动搜集所有数据?...

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位小数的科学计数法如上例所示,可以根据个人爱好改编
与优秀的人做朋友
5楼2018-09-05 19:08:10
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zhaoshf901 的主题更新
信息提示
请填处理意见