简体   繁体   中英

How to change data format in write function in Python?

How to change the data format in f.write function?

loaded_data = 349.00 or 3.00

I want to change data format in write function like %6f in print function.

ex) 349.00 -> 349.000000 , 3.00 -> 3.000000

f = open("test.txt", 'w')
f.write( str.(loaded_data).zfill(?) )  

What is the code that performs above function?

you can use:

>>> loaded_data = 349.00
>>> str.format('{0:.6f}', loaded_data)
'349.000000'

or

>>> loaded_data = 349.00
>>> "%.6f" % loaded_data
'349.000000'

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