简体   繁体   中英

Using format In python not working (Beginner Question)

I have program that is turning sensor measurements into a.xyz file. To do this, it has to write to the file in the form xyz \n I'm using the following line of code

f.write('{0:f} 0 {0:f}\n'.format(xpos,zpos))

xpos and zpos are both floats

I was expecting the output to the file to be

xpos 0 zpos

but instead i'm getting

xpos 0 xpos

I'm not sure why, and I'm not sure any alternative to using format either.

f.write('{0:f} 0 {1:f}\n'.format(xpos,zpos))

An alternative is:

 f.write(str(xpos) +' 0 '+str(zpos)+'\n') 

I think you can do it that way.

f.write(f"{xpos} 0 {ypos}")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM