{"id":3911,"date":"2018-05-05T21:13:44","date_gmt":"2018-05-05T13:13:44","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3911"},"modified":"2018-05-05T21:13:44","modified_gmt":"2018-05-05T13:13:44","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e5%ad%97%e7%ac%a6%e4%b8%b2-%e5%88%97%e8%a1%a8%e6%98%af%e5%90%a6%e4%b8%ba%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3911.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u68c0\u67e5\u5b57\u7b26\u4e32\/\u5217\u8868\u662f\u5426\u4e3a\u7a7a"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6574\u7406\u3001\u8bb0\u5f55\u3001\u5907\u5fd8<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<ol>\n<li>\u4ecedict\u4e2d\u53d6\u503c\u65f6\uff0c\u4e00\u5b9a\u8981\u4f7f\u7528.get()\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u7684\u65b9\u5f0f\uff0c\u907f\u514dKeyError\u5f02\u5e38\uff1b<\/li>\n<li>\u5728\u4f7f\u7528\u524d\u5224\u7a7a or \u52a0\u4e0atry..catch\u8bed\u53e5\u5757\u9884\u9632\u5f02\u5e38\uff1b<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true \">In [50]: resp = {\"total\": 0, \"rows\": []}\r\n    ...: alist = resp.get('rows')\r\n    ...:\r\n\r\nIn [51]: if not alist:\r\n    ...:     print 'hi' #\u5f53alist\u4e3a\u7a7a\u5217\u8868\u65f6\u4f1a\u6267\u884cprint\r\n    ...:\r\nhi\r\n\r\nIn [52]: if alist:\r\n    ...:     print 'hi'\r\n    ...:\r\n\r\nIn [53]: resp.get('rows')\r\nOut[53]: []\r\n\r\nIn [54]: resp.get('row')\r\n\r\nIn [55]: print resp.get('row')\r\nNone\r\n\r\nIn [56]: print resp.get('row', '')\r\n\r\n\r\nIn [57]:<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true\">In [60]: if len(resp['rows']) == 0:\r\n    ...:     print 'hi'\r\n    ...:\r\nhi\r\n\r\nIn [61]: if not resp.get('rows'):\r\n    ...:     print 'hi'\r\n    ...:\r\nhi\r\n\r\nIn [62]: if not resp['rows']:\r\n    ...:     print 'hi'\r\n    ...:\r\nhi\r\n\r\nIn [63]: if not resp['row']:\r\n    ...:     print 'hi'\r\n    ...:\r\n---------------------------------------------------------------------------\r\nKeyError                                  Traceback (most recent call last)\r\n&lt;ipython-input-63-94470f6bb953&gt; in &lt;module&gt;()\r\n----&gt; 1 if not resp['row']:\r\n      2     print 'hi'\r\n\r\nKeyError: 'row'\r\n\r\nIn [64]: if not resp.get('row'):\r\n    ...:     print 'hi'\r\n    ...:\r\nhi\r\n\r\nIn [65]:<\/pre>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/9573244\/most-elegant-way-to-check-if-the-string-is-empty-in-python\">Most elegant way to check if the string is empty in Python?<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#truth-value-testing\">Python\u4e2d\u7684\u771f\u503c\u6d4b\u8bd5<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/9926446\/how-to-check-whether-a-strvariable-is-empty-or-not\" target=\"_blank\" rel=\"noopener\">\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\/\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/53513\/how-do-i-check-if-a-list-is-empty\">\u5982\u4f55\u68c0\u67e5\u4e00\u4e2alist\u662f\u5426\u4e3a\u7a7a\uff1f<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/2972212\/creating-an-empty-list-in-python\">Python\u4e2d\u521b\u5efa\u7a7a\u5217\u8868\u7684\u6700\u5feb\u65b9\u6cd5<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u3001\u8bb0\u5f55\u3001\u5907\u5fd8 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a \u4ecedict\u4e2d\u53d6\u503c\u65f6\uff0c\u4e00\u5b9a\u8981\u4f7f\u7528.get()\u65b9\u6cd5\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[220,8],"class_list":["post-3911","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-dict","tag-python"],"views":3019,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3911","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3911"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3911\/revisions"}],"predecessor-version":[{"id":3917,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3911\/revisions\/3917"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}