【问题标题】:Alignment and non-default floating point representation对齐和非默认浮点表示
【发布时间】:2013-02-27 14:02:32
【问题描述】:

在我的工作中,我经常在开始使用之前检查一些文件的内容,并开始使用检查和一些漂亮的打印来创建 ipython 笔记本来总结我得到的内容。为此,我通常使用printstr.format 在浮点数中获取有序输出或固定数字小数。我能做的总结在这里:

f = 32.4321413432
a = 34121
print('float {ff} and integer {ii}'.format(ff=f, i=i))
print('float {ff:.4f} and integer {ii:d}'.format(ff=f, ii=i))
print('float {ff:>20} and integer {ii:>10}'.format(ff=f, ii=i))

输出

float 32.4321413432 and integer 34121
float 32.4321 and integer 34121
float        32.4321413432 and integer      34121

有时我希望能够一起设置对齐方式和小数位数并得到这样的输出

float        32.4321 and integer      34121

我在{ff:...} 中尝试了.4f>20 的多种组合,但我得到了

ValueError:无效的转换规范

我想要的有可能吗?如果有,语法如何?

【问题讨论】:

    标签: python string format


    【解决方案1】:

    你的意思是这样吗?

    f = 32.4321413432
    i = 34121
    print('float {ff:>20.4f} and integer {ii:>10}'.format(ff=f, ii=i))
    

    产生

    float              32.4321 and integer      34121
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-28
      • 2013-11-18
      • 1970-01-01
      • 2015-03-04
      • 2013-06-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      相关资源
      最近更新 更多