<legend id='6iSWh'><style id='6iSWh'><dir id='6iSWh'><q id='6iSWh'></q></dir></style></legend>

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

      1. <small id='6iSWh'></small><noframes id='6iSWh'>

        • <bdo id='6iSWh'></bdo><ul id='6iSWh'></ul>

        python字符串格式抑制/静默keyerror/indexerror

        时间:2023-10-08

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

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

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

              <bdo id='xctB1'></bdo><ul id='xctB1'></ul>

                  本文介绍了python字符串格式抑制/静默keyerror/indexerror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法使用python string.format,当索引丢失时不会抛出异常,而是插入一个空字符串.

                  Is there a way to use python string.format such that no exception is thrown when an index is missing, instead an empty string is inserted.

                  result = "i am an {error} example string {error2}".format(hello=2,error2="success")
                  

                  这里,结果应该是:

                  "i am an   example string success"
                  

                  现在,python 抛出一个 keyerror 并停止格式化.是否可以改变这种行为?

                  Right now, python throws a keyerror and stops formatting. Is it possible to change this behavior ?

                  谢谢

                  存在 Template.safe_substitute (即使保留模式完整而不是插入空字符串),但 string.format 不能有类似的东西

                  There exists Template.safe_substitute (even that leaves the pattern intact instead of inserting an empty string) , but couldn't something similar for string.format

                  所需的行为类似于 php 中的字符串替换.

                  The desired behavior would be similar to string substitution in php.

                  class Formatter(string.Formatter):
                    def get_value(self,key,args,kwargs):
                      try:
                          if hasattr(key,"__mod__"):
                              return args[key]
                          else:
                              return kwargs[key]
                      except:
                          return ""
                  

                  这似乎提供了所需的行为.

                  This seems to provide the desired behavior.

                  推荐答案

                  str.format() 不需要映射对象.试试这个:

                  str.format() doesn't expect a mapping object. Try this:

                  from collections import defaultdict
                  
                  d = defaultdict(str)
                  d['error2'] = "success"
                  s = "i am an {0[error]} example string {0[error2]}"
                  print s.format(d)
                  

                  您使用返回"的 str() 工厂创建一个 defaultdict.然后你为 defaultdict 创建一个键.在格式字符串中,您访问传递的第一个对象的键.这样做的好处是允许您传递其他键和值,只要您的 defaultdict 是 format() 的第一个参数.

                  You make a defaultdict with a str() factory that returns "". Then you make one key for the defaultdict. In the format string, you access keys of the first object passed. This has the advantage of allowing you to pass other keys and values, as long as your defaultdict is the first argument to format().

                  另外,请参阅 http://bugs.python.org/issue6081

                  这篇关于python字符串格式抑制/静默keyerror/indexerror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python - 使用 pandas 格式化 Excel 单元格 下一篇:早于 2.6 的 Python 版本中的字符串格式

                  相关文章

                  最新文章

                    <bdo id='sbfG8'></bdo><ul id='sbfG8'></ul>

                  <tfoot id='sbfG8'></tfoot>

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

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

                    1. <legend id='sbfG8'><style id='sbfG8'><dir id='sbfG8'><q id='sbfG8'></q></dir></style></legend>