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

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

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

        • <bdo id='d8EFL'></bdo><ul id='d8EFL'></ul>

        Python3 - 在字符串格式化程序参数中使用变量

        时间:2023-10-08
      1. <small id='oMwJI'></small><noframes id='oMwJI'>

              <tbody id='oMwJI'></tbody>
                <bdo id='oMwJI'></bdo><ul id='oMwJI'></ul>
                <legend id='oMwJI'><style id='oMwJI'><dir id='oMwJI'><q id='oMwJI'></q></dir></style></legend>

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

                  本文介绍了Python3 - 在字符串格式化程序参数中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在打印一些格式化的列.我想使用以下变量来设置我的 .format 参数中的长度

                  I have some formatted columns that I'm printing. I would like to use the following variables to set the lengths in my .format arguments

                  number_length = 5
                  name_length = 24
                  viewers_length = 9
                  

                  我有

                  print('{0:<5}{1:<24}{2:<9}'.format(' #','channel','viewers'), end = '')
                  

                  理想情况下,我想要类似的东西

                  Ideally I would like something like

                  print('{0:<number_length}{1:<name_length}{2:<viewers_length}'.format(
                       ' #','channel','viewers'), end = '')
                  

                  但这给了我一个无效的字符串格式化错误.

                  But this gives me an invalid string formatter error.

                  我曾尝试在变量和括号前加上 %,但没有成功.

                  I have tried with % before the variables and parenthesis, but have had no luck.

                  推荐答案

                  你需要:

                  1. 也将名字用大括号括起来;和
                  2. 将宽度作为关键字参数传递给 str.format.

                  例如:

                  >>> print("{0:>{number_length}}".format(1, number_length=8))
                         1
                  

                  你也可以使用字典解包:

                  You can also use dictionary unpacking:

                  >>> widths = {'number_length': 8}
                  >>> print("{0:>{number_length}}".format(1, **widths))
                         1
                  

                  str.format 不会在本地范围内查找适当的名称;它们必须显式传递.

                  str.format won't look in the local scope for appropriate names; they must be passed explicitly.

                  对于您的示例,这可以像这样工作:

                  For your example, this could work like:

                  >>> widths = {'number_length': 5,
                                'name_length': 24,
                                'viewers_length': 9}
                  >>> template= '{0:<{number_length}}{1:<{name_length}}{2:<{viewers_length}}'
                  >>> print(template.format('#', 'channel', 'visitors', end='', **widths))
                  #    channel                 visitors
                  

                  (请注意,end 和任何其他显式关键字参数必须在 **widths 之前.)

                  (Note that end, and any other explicit keyword arguments, must come before **widths.)

                  这篇关于Python3 - 在字符串格式化程序参数中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在python中按可变长度格式化 下一篇:如何将 pandas 数据框的数据类型更改为具有定义格

                  相关文章

                  最新文章

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

                  1. <small id='JRKXG'></small><noframes id='JRKXG'>

                      <tfoot id='JRKXG'></tfoot>
                        <bdo id='JRKXG'></bdo><ul id='JRKXG'></ul>
                      <legend id='JRKXG'><style id='JRKXG'><dir id='JRKXG'><q id='JRKXG'></q></dir></style></legend>