python - Round, align and print list of floats with format() -


i need write several floats file using format() method. want round floats given number of decimal places and write them aligned @ same time.

here's mwe:

a = 546.35642 b = 6785.35416 c = 12.5235 d = 13.643241  line = [str('{:.2f}'.format(a)),     str('{:.4f}'.format(b)),     str('{:.5f}'.format(c)),     str('{:.3f}'.format(d))]  open('format_test.dat', "a") f_out:     f_out.write('''{:>10} {:>15} {:>16} {:>15}'''.format(*line))     f_out.write('\n') 

this gets job done seems awfully convoluted me. there better way using format()?

you can add .#f in format alignment.

with open('format_test.dat', "a") f_out:     f_out.write('''{:>10.2f} {:>15.4f} {:>16.5f} {:>15.3f}'''.format(a, b, c, d))     f_out.write('\n') 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -