久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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 class="qwawimqqmiuu" id='xctB1'><tfoot id='xctB1'></tfoot><dl id='xctB1'><fieldset id='xctB1'></fieldset></dl></div>

<tfoot id='xctB1'></tfoot>
  • <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 suppress/silent keyerror/indexerror(python字符串格式抑制/靜默keyerror/indexerror)
        <legend id='6iSWh'><style id='6iSWh'><dir id='6iSWh'><q id='6iSWh'></q></dir></style></legend>

          <tbody id='6iSWh'></tbody>

                <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 class="qwawimqqmiuu" 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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  有沒有辦法使用python string.format,當(dāng)索引丟失時不會拋出異常,而是插入一個空字符串.

                  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")
                  

                  這里,結(jié)果應(yīng)該是:

                  "i am an   example string success"
                  

                  現(xiàn)在,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() 工廠創(chuàng)建一個 defaultdict.然后你為 defaultdict 創(chuàng)建一個鍵.在格式字符串中,您訪問傳遞的第一個對象的鍵.這樣做的好處是允許您傳遞其他鍵和值,只要您的 defaultdict 是 format() 的第一個參數(shù).

                  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

                  這篇關(guān)于python字符串格式抑制/靜默keyerror/indexerror的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機(jī)器人?)
                  Discord bot isn#39;t responding to commands(Discord 機(jī)器人沒有響應(yīng)命令)
                  Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關(guān)于我嗎?Discord 機(jī)器人的功能?(不和諧.py))
                  message.channel.id Discord PY(message.channel.id Discord PY)
                  How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機(jī)器人?)
                  discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)

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

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

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

                          <tbody id='sbfG8'></tbody>
                        <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 class="qwawimqqmiuu" id='sbfG8'><tfoot id='sbfG8'></tfoot><dl id='sbfG8'><fieldset id='sbfG8'></fieldset></dl></div>

                          • <legend id='sbfG8'><style id='sbfG8'><dir id='sbfG8'><q id='sbfG8'></q></dir></style></legend>
                            主站蜘蛛池模板: 精品一区二区三区中文字幕 | 色香蕉在线 | 亚洲一级黄色 | 天天影视亚洲综合网 | 国产精品爱久久久久久久 | 四虎影视免费在线 | 亚洲毛片| 国产激情精品 | 波波电影院一区二区三区 | 精品欧美乱码久久久久久1区2区 | 亚洲成人中文字幕 | 毛片网络 | 91社区在线观看 | 欧美视频在线免费 | 91成人免费看片 | 成人免费大片黄在线播放 | 久久91 | 麻豆毛片| 午夜影院在线观看 | 在线播放精品视频 | 欧美国产精品一区二区三区 | 狠狠婷婷综合久久久久久妖精 | 国产一区二区三区在线视频 | 在线看片网站 | 久久一二区 | 亚洲在线一区二区三区 | 欧美一区二区三区在线观看视频 | 天堂在线1 | 亚洲视频www| 最新av在线网址 | 一区二区三区免费看 | 亚洲精品永久免费 | 亚洲一区二区久久久 | 国产成人精品a视频 | 一个色在线 | 日韩在线国产 | 亚洲婷婷一区 | 成年人免费网站 | 亚洲视频在线一区 | 欧美在线综合 | 黑人精品欧美一区二区蜜桃 |