简体   繁体   中英

Python: Write a list of numeric values on a single row with a repeated format

I have a list of numeric values, let's say:

draw6 = [0.13234, 0.14235, 0.15235, 0.16233]

I want to write the values in this to a file, the same way the print statement below does:

print ('{:10.4f} '*len(draw6)).format(*draw6)

The print statement works, but I need to use a write statement because I want to write to a file. The write statement doesn't have "format" as an attribute.

So

file.write('{:10.4f} '*len(draw6)).format(*draw6)

does not work.

How do I mimic the print statement in a write statement? I've tried tens of options but none of them work. I was very happy when the print statement worked, but then very disappointed when it didn't work with write.

Many thanks, Gijs

Do it as follows:

s = ('{:10.4f} '*len(draw6)).format(*draw6)    # s = '    0.1323     0.1424     0.1524     0.1623'
file.write(s)

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