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

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

        <tfoot id='DjsBx'></tfoot>
      1. <small id='DjsBx'></small><noframes id='DjsBx'>

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

        python string format suppress/silent keyerror/indexerror(python字符串格式抑制/静默keyerror/indexerror)
      2. <small id='54AK5'></small><noframes id='54AK5'>

            <tbody id='54AK5'></tbody>
        • <tfoot id='54AK5'></tfoot>

            <legend id='54AK5'><style id='54AK5'><dir id='54AK5'><q id='54AK5'></q></dir></style></legend>

              <i id='54AK5'><tr id='54AK5'><dt id='54AK5'><q id='54AK5'><span id='54AK5'><b id='54AK5'><form id='54AK5'><ins id='54AK5'></ins><ul id='54AK5'></ul><sub id='54AK5'></sub></form><legend id='54AK5'></legend><bdo id='54AK5'><pre id='54AK5'><center id='54AK5'></center></pre></bdo></b><th id='54AK5'></th></span></q></dt></tr></i><div id='54AK5'><tfoot id='54AK5'></tfoot><dl id='54AK5'><fieldset id='54AK5'></fieldset></dl></div>
                • <bdo id='54AK5'></bdo><ul id='54AK5'></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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                    <bdo id='3veZx'></bdo><ul id='3veZx'></ul>
                      <tbody id='3veZx'></tbody>

                    <legend id='3veZx'><style id='3veZx'><dir id='3veZx'><q id='3veZx'></q></dir></style></legend>

                      1. <small id='3veZx'></small><noframes id='3veZx'>

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