{"id":2754,"date":"2016-06-24T20:23:40","date_gmt":"2016-06-24T12:23:40","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2754"},"modified":"2016-06-24T20:23:40","modified_gmt":"2016-06-24T12:23:40","slug":"python%e7%9a%84%e3%80%8czero-length-field-name-in-format%e3%80%8d%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2754.html","title":{"rendered":"Python\u7684\u300czero length field name in format\u300d\u9519\u8bef"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u5728\u5199\u4e00\u4e2asplitter\u7684\u65f6\u5019\u603b\u662f\u5f97\u4e0d\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u672c\u5730\u6d4b\u8bd5\u53c8\u603b\u662fOK\u7684\uff0c\u6700\u540e\u7684\u6700\u540e\u53d1\u73b0\u662f\u56e0\u4e3a\u300czero length field name in format\u300d\u9519\u8bef\u3002\u7531Python\u7248\u672c\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\uff0c\u8c03\u8bd5\u8d77\u6765\u771f\u7684\u662f\u5f88\u5751\u7239\u554a\uff01<\/p>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>Python\u5b98\u65b9\u63a8\u8350\u4f7f\u7528 str.format() \u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\uff0c\u6240\u4ee5\u6211\u4e5f\u8ddf\u968f\u6f6e\u6d41\u4f7f\u7528 <code>\"{}\\n\".format(x)<\/code> \u7684\u65b9\u5f0f\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5e73\u65f6\u4e5f\u6ca1\u6709\u78b0\u5230\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u662f\u8fd9\u6b21\uff0c\u56e0\u4e3a\u4e0d\u591f\u8c28\u614e\uff0c\u72af\u4e86\u4e2a\u9519\u8bef\u628a\u81ea\u5df1\u7ed9\u5751\u4e86\u2026\u2026<\/p>\n<p><strong><span style=\"color: #0000ff;\">\u5728Python2(&gt;=2.7)\u548cPython3(&gt;=3.1)\u4e2d\uff0cstr.format()\u65b9\u6cd5\u4e2d\u7684\u4f4d\u7f6e\u53c2\u6570\u9ed8\u8ba4\u662f\u53ef\u4ee5\u7701\u7565\u7684\uff0c\u4f46\u662f\u5728Python2(&lt;2.7)\u548cPython3(&lt;3.1)\u4e2d\uff0c\u4f60\u5fc5\u987b\u663e\u793a\u6307\u5b9a\uff08\u4e0b\u6807\u4ece0\u5f00\u59cb\uff09\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u300czero length field name in format\u300d\u9519\u8bef\u3002<\/span><\/strong><\/p>\n<pre class=\"lang:default decode:true \">\"{}CMD\".format(os.sep)    #\u5728Python 2.7\u4e2d\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\n#\n\"{0}CMD\".format(os.sep)  #\u5728Python&lt;=2.6\/3.1\u4e2d\uff0c\u5fc5\u987b\u663e\u793a\u6307\u5b9a\u4e0b\u6807\uff0c\u5373\u4fbf\u53ea\u6709\u4e00\u4e2a\u5143\u7d20<\/pre>\n<p>\u6211\u5f88\u5c11\u5728Python2.6\u7684\u73af\u5883\u4e2d\u7f16\u7a0b\uff0c\u7b14\u8bb0\u672c\u4e0a\u88c5\u4e86Python2.7\u548cPython3.5\uff0c\u5373\u4fbf\u662f\u5728CentOS 6.x\u7684\u670d\u52a1\u5668\u4e0a\u4e5f\u4f1a\u624b\u52a8\u7f16\u8bd1\u5b89\u88c5\u4e00\u4e2aPython2.7\uff0c\u7136\u540e\u5c06\u7f16\u8bd1\u5b89\u88c5\u7684Python2.7\u653e\u5728\u81ea\u5df1\u7684\u73af\u5883\u53d8\u91cf$PATH\u7684\u6700\u524d\u9762\u6240\u4ee5\u81ea\u5df1\u7528\u7684\u5c31\u662fPython2.7\uff0c\u670d\u52a1\u5668\u4e0a\u7684\u547d\u4ee4\u6bd4\u5982yum\u8fd9\u79cd\u7528\u7684\u5c31\u662f\u9ed8\u8ba4\u7684Python2.6.6\u3002\u4f46\u662f\uff0csplitter\u662f\u653e\u4e0bHadoop\u96c6\u7fa4\u4e0a\u8dd1\u7684\uff0c\u7528\u7684\u662fPython2.6.6\u73af\u5883\uff0c\u7136\u540e\u5c31\u60b2\u5267\u4e86\u2026\u2026<\/p>\n<p><strong><span style=\"color: #ff0000;\">\u867d\u7136\u8bf4\u8fd9\u662fPython\u7684\u4e00\u4e2abug\uff0c\u4f46\u662f\u81ea\u5df1\u4e5f\u5fc5\u987b\u660e\u786e<span style=\"color: #0000ff;\">\u300c\u4f60\u5199\u7684\u7a0b\u5e8f\u4f1a\u8fd0\u884c\u5728\u4ec0\u4e48\u73af\u5883\u4e2d\uff1f\u662f\u5426\u6709\u7528\u5230\u548c\u7248\u672c\u5f3a\u76f8\u5173\u7684\u7279\u6027\uff1f\u7528\u5f97\u662f\u5426\u6b63\u786e\uff1f\u6d4b\u8bd5\u662f\u5426\u5168\u9762\u3001\u5b8c\u6574\uff1f\u300d<\/span>\uff0c\u5426\u5219\uff0c\u4f60\u81ea\u5df1\u5c31\u662f\u5728\u7ed9\u81ea\u5df1\u548c\u522b\u4eba\u6316\u4e00\u4e2a\u5927\u5751\uff01\u8fd8\u4e0d\u5bb9\u6613\u722c\u51fa\u6765\u3002<\/span><\/strong><\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">Changed in version 3.1: The positional argument specifiers can be omitted, so &#8216;{} {}&#8217; is equivalent to &#8216;{0} {1}&#8217;.<\/p>\n<\/blockquote>\n<p>\u53e6\uff1a\u867d\u7136\u8bf4\u4e00\u822c\u5efa\u8bae\u662f\u5c11\u53bb\u8003\u8651Python2.6\u7684\u73af\u5883\uff08\u5347\u7ea7\u5230Python2.7\u6216Python3.2\uff09\uff0c\u4e00\u5207\u5411\u524d\u770b\uff1b\u4f46\u662f\uff0cCentOS 6.x\u9ed8\u8ba4\u88c5\u7684\u5c31\u662fPython2.6.6\uff0c\u4e5f\u5f88\u5c11\u6709\u516c\u53f8\u4f1a\u53bb\u6574\u4f53\u5347\u7ea7\u670d\u52a1\u5668\u4e0a\u7684Python\u7248\u672c\uff0c\u6240\u4ee5\u5fd8\u4e86\u8fd9\u4e2a\u5efa\u8bae\u5427\uff0c\u81ea\u5df1\u591a\u7559\u4e2a\u5fc3\u773c\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10054122\/valueerror-zero-length-field-name-in-format-python\">http:\/\/stackoverflow.com\/questions\/10054122\/valueerror-zero-length-field-name-in-format-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5446964\/valueerror-zero-length-field-name-in-format-error-in-python-3-0-3-1-3-2\">http:\/\/stackoverflow.com\/questions\/5446964\/valueerror-zero-length-field-name-in-format-error-in-python-3-0-3-1-3-2<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/24739980\/python-zero-length-field-name-in-format\/24740068#24740068\">http:\/\/stackoverflow.com\/questions\/24739980\/python-zero-length-field-name-in-format\/24740068#24740068<\/a><\/li>\n<li>#<\/li>\n<li><a href=\"http:\/\/blog.xiayf.cn\/2013\/01\/26\/python-string-format\/\">http:\/\/blog.xiayf.cn\/2013\/01\/26\/python-string-format\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#str.format\">https:\/\/docs.python.org\/2\/library\/stdtypes.html#str.format<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/string.html#formatstrings\">https:\/\/docs.python.org\/2\/library\/string.html#formatstrings<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u5199\u4e00\u4e2asplitter\u7684\u65f6\u5019\u603b\u662f\u5f97\u4e0d\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u672c\u5730\u6d4b\u8bd5\u53c8\u603b\u662fOK\u7684\uff0c\u6700\u540e\u7684\u6700 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[8,634],"class_list":["post-2754","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-python","tag-str-format"],"views":10537,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2754"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2754\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}