1. <tfoot id='FuHYa'></tfoot>
      <bdo id='FuHYa'></bdo><ul id='FuHYa'></ul>
    <legend id='FuHYa'><style id='FuHYa'><dir id='FuHYa'><q id='FuHYa'></q></dir></style></legend>
      <i id='FuHYa'><tr id='FuHYa'><dt id='FuHYa'><q id='FuHYa'><span id='FuHYa'><b id='FuHYa'><form id='FuHYa'><ins id='FuHYa'></ins><ul id='FuHYa'></ul><sub id='FuHYa'></sub></form><legend id='FuHYa'></legend><bdo id='FuHYa'><pre id='FuHYa'><center id='FuHYa'></center></pre></bdo></b><th id='FuHYa'></th></span></q></dt></tr></i><div id='FuHYa'><tfoot id='FuHYa'></tfoot><dl id='FuHYa'><fieldset id='FuHYa'></fieldset></dl></div>

      <small id='FuHYa'></small><noframes id='FuHYa'>

    1. Python格式字符串中的宽度变量

      时间:2023-10-09

      <legend id='5po1d'><style id='5po1d'><dir id='5po1d'><q id='5po1d'></q></dir></style></legend>
        <tbody id='5po1d'></tbody>
        <tfoot id='5po1d'></tfoot>

        • <i id='5po1d'><tr id='5po1d'><dt id='5po1d'><q id='5po1d'><span id='5po1d'><b id='5po1d'><form id='5po1d'><ins id='5po1d'></ins><ul id='5po1d'></ul><sub id='5po1d'></sub></form><legend id='5po1d'></legend><bdo id='5po1d'><pre id='5po1d'><center id='5po1d'></center></pre></bdo></b><th id='5po1d'></th></span></q></dt></tr></i><div id='5po1d'><tfoot id='5po1d'></tfoot><dl id='5po1d'><fieldset id='5po1d'></fieldset></dl></div>

            <small id='5po1d'></small><noframes id='5po1d'>

              <bdo id='5po1d'></bdo><ul id='5po1d'></ul>

              1. 本文介绍了Python格式字符串中的宽度变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                为了打印表格数据的标题,我只想使用一个格式字符串 line 和一个列宽规范 w1, w2, w3 (如果可能的话,甚至是 w = x, y, z.)

                In order to print a header for tabular data, I'd like to use only one format string line and one spec for column widths w1, w2, w3 (or even w = x, y, z if possible.)

                我看过 this 但 tabulate 等不要让我像 format 那样证明列中的内容.

                I've looked at this but tabulate etc. don't let me justify things in the column like format does.

                这种方法有效:

                head = 'eggs', 'bacon', 'spam'  
                w1, w2, w3 = 8, 7, 10  # column widths  
                line = '  {:{ul}>{w1}}  {:{ul}>{w2}}  {:{ul}>{w3}}'  
                under = 3 * '='  
                print line.format(*head, ul='', w1=w1, w2=w2, w3=w3)  
                print line.format(*under, ul='=', w1=w1, w2=w2, w3=w3)  
                

                我必须在格式字符串中有单独的名称作为宽度 {w1}, {w2}, ...吗?{w[1]}{w[2]} 等尝试给出 KeyErrorkeyword can't be an表达式.

                Must I have individual names as widths {w1}, {w2}, ... in the format string? Attempts like {w[1]}, {w[2]}, give either KeyError or keyword can't be an expression.

                另外我认为 w1=w1, w2=w2, w3=w3 不是很简洁.有没有更好的办法?

                Also I think the w1=w1, w2=w2, w3=w3 is not very succinct. Is there a better way?

                推荐答案

                这是 jonrsharpe 对我的 OP 的评论,旨在可视化正在发生的事情.

                This is jonrsharpe's comment to my OP, worked out so as to visualise what's going on.

                line = '  {:{ul}>{w1}}  {:{ul}>{w2}}  {:{ul}>{w3}}'
                under = 3 * '_'
                
                head = 'sausage', 'rat', 'strawberry tart'
                
                # manual dict 
                v = {'w1': 8, 'w2':5, 'w3': 17}
                print line.format(*under, ul='_', **v)
                
                # auto dict
                widthl = [8, 7, 9]
                x = {'w{}'.format(index): value for index, value in enumerate(widthl, 1)}
                print line.format(*under, ul='_', **x)     
                

                关键是我希望能够快速重新排列标题,而无需调整格式字符串.auto dict 很好地满足了这个要求.

                The point is that I want to be able to quickly rearrange the header without having to tweak the format string. The auto dict meets that requirement very nicely.

                至于以这种方式填充字典:哇!

                As for filling a dict in this way: WOW!

                这篇关于Python格式字符串中的宽度变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python - 在对齐的列中打印 CSV 字符串列表 下一篇:使用变量格式化对齐?

                相关文章

                最新文章

              2. <small id='Cv4FC'></small><noframes id='Cv4FC'>

                <legend id='Cv4FC'><style id='Cv4FC'><dir id='Cv4FC'><q id='Cv4FC'></q></dir></style></legend>

                  <i id='Cv4FC'><tr id='Cv4FC'><dt id='Cv4FC'><q id='Cv4FC'><span id='Cv4FC'><b id='Cv4FC'><form id='Cv4FC'><ins id='Cv4FC'></ins><ul id='Cv4FC'></ul><sub id='Cv4FC'></sub></form><legend id='Cv4FC'></legend><bdo id='Cv4FC'><pre id='Cv4FC'><center id='Cv4FC'></center></pre></bdo></b><th id='Cv4FC'></th></span></q></dt></tr></i><div id='Cv4FC'><tfoot id='Cv4FC'></tfoot><dl id='Cv4FC'><fieldset id='Cv4FC'></fieldset></dl></div>
                  <tfoot id='Cv4FC'></tfoot>
                      <bdo id='Cv4FC'></bdo><ul id='Cv4FC'></ul>