{"id":1488,"date":"2014-10-17T16:08:27","date_gmt":"2014-10-17T16:08:27","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1488"},"modified":"2014-10-17T16:08:27","modified_gmt":"2014-10-17T16:08:27","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1488.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9"},"content":{"rendered":"<p><strong>Python<\/strong><strong>\u751f\u6210<\/strong><strong>GUID<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; import uuid\n&gt;&gt;&gt; uuid.uuid1()\nUUID('5a35a426-f7ce-11dd-abd2-0017f227cfc7')<\/pre>\n<hr \/>\n<p><strong>#Python<\/strong><strong>\u4e2d\u7684<\/strong><strong>string<\/strong><strong>\u5bf9\u8c61\u6709\u51e0\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u7528\u6765\u8f93\u51fa\u5404\u79cd\u4e0d\u540c\u7684\u5b57\u7b26\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">string.ascii_letters\u00a0\u00a0\u00a0 #\u8f93\u51faascii\u7801\u7684\u6240\u6709\u5b57\u7b26\nstring.digits\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u8f93\u51fa '0123456789'.\nstring.punctuation\u00a0\u00a0\u00a0\u00a0\u00a0 #ascii\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\nprint string.ascii_letters\nprint string.digits\nprint string.punctuation\n\n#\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n0123456789\n!\"#$%&amp;'()*+,-.\/:;&lt;=&gt;?@[]^_`{|}~<\/pre>\n<p><strong>#<\/strong><strong>\u4e0b\u9762\u7684\u4ee3\u7801\u7528\u4e8e\u751f\u6210\u968f\u673a\u5bc6\u7801\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">import string\nfrom random import *\ncharacters = string.ascii_letters + string.punctuation + string.digits\npassword = \"\".join(choice(characters) for x in range(randint(8, 16)))\nprint password<\/pre>\n<p><strong>\u7528<\/strong><strong>Python<\/strong><strong>\u5224\u65ad\u53d8\u91cf\u7c7b\u578b<\/strong><\/p>\n<p><a href=\"http:\/\/segmentfault.com\/q\/1010000000127305\" target=\"_blank\">python\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350\u4f7f\u7528type()\u65b9\u6cd5<\/a>\uff1f<\/p>\n<p>\u5b9e\u9645\u4e0a\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u7528isinstance\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true\">a = 111\nisinstance(a, int)\nTrue<\/pre>\n<p>isinstance \u548c type\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">class A:\n    pass\nclass B(A):\n    pass\n\nisinstance(A(), A)\u00a0 # returns True\ntype(A()) == A\u00a0\u00a0\u00a0\u00a0\u00a0 # returns True\nisinstance(B(), A)\u00a0\u00a0\u00a0 # returns True\ntype(B()) == A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # returns False<\/pre>\n<p>\u533a\u522b\u5c31\u662f \u5bf9\u4e8esubclass\u4e4b\u7c7b\u7684 type \u5c31\u4e0d\u7701\u4e8b\u4e86\u3002<\/p>\n<p><strong>\u53c2\u8003\u94fe\u63a5<\/strong>\uff1apython\u5224\u65ad\u53d8\u91cf\u7c7b\u578b\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u63a8\u8350\u4f7f\u7528type()\u65b9\u6cd5 &#8211; SegmentFault <a href=\"http:\/\/segmentfault.com\/q\/1010000000127305\" target=\"_blank\">http:\/\/segmentfault.com\/q\/1010000000127305<\/a><\/p>\n<hr \/>\n<ul>\n<li><a href=\"http:\/\/blog.csdn.net\/gukesdo\/article\/details\/7064868\" target=\"_blank\">Python \u5224\u65ad\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/kiki113\/article\/details\/4152964\" target=\"_blank\">Python\u7684\u7c7b\u578b\u5224\u65ad\uff1aisinstance<\/a><\/li>\n<\/ul>\n<p>Python\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b \uff0c\u5229\u7528type\u51fd\u6570\uff1a<\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt;lst = [1, 2, 3]\n&gt;&gt;&gt;type(lst)\n&lt;type 'list'&gt;<\/pre>\n<p>\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528isinstance\u51fd\u6570\uff0c\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u4e00\u4e2a\u5df2\u77e5\u7684\u7c7b\u578b\u3002isinstance\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>\u00a0 isinstance(object, class-or-type-or-tuple) -&gt; bool<\/p>\n<p>Return whether an object is an instance of a class or of a subclass thereof.<\/p>\n<p>With a type as second argument, return whether that is the object&#8217;s type.<\/p>\n<p>The form using a tuple, isinstance(x, (A, B, &#8230;)), is a shortcut for isinstance(x, A)<\/p>\n<p>or isinstance(x, B) or &#8230; (etc.).<\/p><\/blockquote>\n<p>\u5176\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u7c7b\u578b\u540d\u6216\u7c7b\u578b\u540d\u7684\u4e00\u4e2a\u5217\u8868\u3002\u5176\u8fd4\u56de\u503c\u4e3a\u5e03\u5c14\u578b\u3002<\/p>\n<p>\u82e5\u5bf9\u8c61\u7684\u7c7b\u578b\u4e0e\u53c2\u6570\u4e8c\u7684\u7c7b\u578b\u76f8\u540c\u5219\u8fd4\u56deTrue\u3002\u82e5\u53c2\u6570\u4e8c\u4e3a\u4e00\u4e2a\u5143\u7ec4\uff0c\u5219\u82e5\u5bf9\u8c61\u7c7b\u578b\u4e0e\u5143\u7ec4\u4e2d\u7c7b\u578b\u540d\u4e4b\u4e00\u76f8\u540c\u5373\u8fd4\u56deTrue\u3002<\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt;isinstance(lst, list)\n\u00a0 Trueisinstance(lst, (int, str, list))\n\u00a0 True\n&gt;&gt;&gt;isinstance(lst, (int, str, list))\n\u00a0 True<\/pre>\n<p>\u8fd9\u4e5f\u7b97Python\u7684\u5f3a\u5927\u4e4b\u4e00\u5427\uff01<\/p>\n<hr \/>\n<p><strong>Python<\/strong><strong>\u4e2d\u7684<\/strong><strong>dict<\/strong><strong>\u548c<\/strong><strong>json<\/strong><strong>\u683c\u5f0f\u7684\u4e00\u4e9b<\/strong><\/p>\n<p><a href=\"http:\/\/www.cnblogs.com\/coser\/archive\/2011\/12\/14\/2287739.html\" target=\"_blank\">Json\u6982\u8ff0\u4ee5\u53capython\u5bf9json\u7684\u76f8\u5173\u64cd\u4f5c<\/a><\/p>\n<p>\u5728Python\u6807\u51c6\u5e93\u7684json\u5305\u4e2d\uff0c\u63d0\u4f9b\u4e86JSONEncoder\u548cJSONDecoder\u4e24\u4e2a\u7c7b\u6765\u5b9e\u73b0Json\u5b57\u7b26\u4e32\u548cdict\u7c7b\u578b\u6570\u636e\u7684\u4e92\u76f8\u8f6c\u6362\u3002<\/p>\n<p>Python \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5b57\u5178dict<\/p>\n<pre class=\"lang:default decode:true\">JSON\u5230\u5b57\u5178\u8f6c\u5316\uff1a\ndictinfo = simplejson.loads(json_str)\u00a0\u00a0 #\u8f93\u51fadict\u7c7b\u578b\n\n\u5b57\u5178\u5230JSON\u8f6c\u5316\uff1a\njsoninfo = simplejson.dumps(dict)\u00a0\u00a0 #\u8f93\u51fastr\u7c7b\u578b<\/pre>\n<p>\u6bd4\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true\">info = {'name' : 'jay', 'sex' : 'male', 'age': 22}\njsoninfo = simplejson.dumps(info)\nprint jsoninfo\nprint type(jsoninfo)<\/pre>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/kaituorensheng\/archive\/2013\/05\/23\/3096028.html\" target=\"_blank\">python strip()\u51fd\u6570<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/pylemon\/archive\/2011\/05\/18\/2050179.html\" target=\"_blank\">Python strip lstrip rstrip\u4f7f\u7528\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"http:\/\/zhidao.baidu.com\/question\/1830065770242120020.html\" target=\"_blank\">python\u5982\u4f55\u5224\u65ad\u4e00\u6bb5\u5b57\u7b26\u4e32\u662f\u5426\u662fjson\u683c\u5f0f\u7684\u3002_\u767e\u5ea6\u77e5\u9053<\/a><\/li>\n<li><a href=\"http:\/\/www.01happy.com\/python-json-encode-and-decode\/\" target=\"_blank\">python\u4e2djson\u683c\u5f0f\u6570\u636e\u7684\u7f16\u7801\u548c\u89e3\u7801<\/a><\/li>\n<\/ul>\n<p><strong>Python<\/strong><strong>\u4e2d\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/dabiao\/archive\/2010\/03\/07\/1680096.html\" target=\"_blank\">Python\u5b66\u4e60\u7b14\u8bb0\u4e00:\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/huangcong\/archive\/2011\/08\/29\/2158268.html\" target=\"_blank\">Python \u5b57\u7b26\u4e32\u64cd\u4f5c\uff08string\u66ff\u6362\u3001\u5220\u9664\u3001\u622a\u53d6\u3001\u590d\u5236\u3001\u8fde\u63a5\u3001\u6bd4\u8f83\u3001\u67e5\u627e\u3001\u5305\u542b\u3001\u5927\u5c0f\u5199\u8f6c\u6362\u3001\u5206\u5272\u7b49\uff09<\/a><\/li>\n<li><a href=\"http:\/\/www.2cto.com\/kf\/201207\/139772.html\" target=\"_blank\">python\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879<\/a><\/li>\n<\/ul>\n<hr \/>\n<p><strong>\u5728<\/strong><strong>Python<\/strong><strong>\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u7684<\/strong><strong>2<\/strong><strong>\u79cd\u65b9\u6cd5<\/strong><\/p>\n<ul>\n<li>\u662f\u7528\u5b57\u7b26\u4e32\u672c\u8eab\u7684replace()\u65b9\u6cd5\u3002<\/li>\n<li>\u7528\u6b63\u5219\u7684sub()\u51fd\u6570\u6765\u66ff\u6362\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7528\u4e2a\u4f8b\u5b50\u6765\u5b9e\u9a8c\u4e0b\uff1a<br \/>\na = &#8216;hello word&#8217;<br \/>\n\u6211\u628aa\u5b57\u7b26\u4e32\u91cc\u7684word\u66ff\u6362\u4e3apython\u3002<\/p>\n<p><strong>1.<\/strong><strong>\u7528\u5b57\u7b26\u4e32\u672c\u8eab\u7684<\/strong><strong>replace<\/strong><strong>\u65b9\u6cd5\uff1a<\/strong><strong><br \/>\n<\/strong>a = a.replace(&#8216;word&#8217;, &#8216;python&#8217;)<br \/>\n\u8f93\u51fa\u7684\u7ed3\u679c\u662fhello python<\/p>\n<p><strong>2.<\/strong><strong>\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b8c\u6210\u66ff\u6362\uff1a<\/strong><strong><br \/>\n<\/strong>import re<br \/>\nstrinfo = re.compile(&#8216;word&#8217;)<br \/>\nb = strinfo.sub(&#8216;python&#8217;, a)<br \/>\nprint b<br \/>\n\u8f93\u51fa\u7684\u7ed3\u679c\u4e5f\u662fhello python<\/p>\n<p>\u81f3\u4e8e\u7528\u54ea\u4e2a\u65b9\u6cd5\u7684\u8bdd\uff0c\u770b\u4f60\u81ea\u5df1\u7684\u9009\u62e9\u4e86\uff08\u987a\u4fbf\u8bf4\u4e00\u53e5\uff0c\u5728Python\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u81ea\u8eab\u7684\u65b9\u6cd5\u901f\u5ea6\u662f\u6700\u5feb\u7684\uff09\u3002<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h5>Python\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5<\/h5>\n<h6>\u65b9\u6cd51\uff1a\u76f4\u63a5\u901a\u8fc7\u52a0\u53f7\u64cd\u4f5c\u7b26\u76f8\u52a0<\/h6>\n<pre class=\"lang:default decode:true\">foobar = 'foo' + 'bar'<\/pre>\n<p>\u4f4e\u6548\u539f\u56e0\uff1a\u5728\u4f7f\u7528&#8221;+&#8221;\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u6bcf\u6b21\u8fde\u63a5\u4e00\u6b21\uff0c\u5c31\u8981\u91cd\u65b0\u5f00\u8f9f\u7a7a\u95f4\uff0c\u7136\u540e\u628a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u518d\u653e\u5165\u65b0\u7684\u7a7a\u95f4\uff0c\u518d\u4e00\u6b21\u5faa\u73af\uff0c\u53c8\u8981\u5f00\u8f9f\u65b0\u7684\u7a7a\u95f4\uff0c\u628a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\u653e\u5165\u65b0\u7684\u7a7a\u95f4\uff0c\u5982\u6b64\u53cd\u590d\uff0c\u5185\u5b58\u64cd\u4f5c\u6bd4\u8f83\u9891\u7e41\uff0c\u6bcf\u6b21\u90fd\u8981\u8ba1\u7b97\u5185\u5b58\u7a7a\u95f4\uff0c\u7136\u540e\u5f00\u8f9f\u5185\u5b58\u7a7a\u95f4\uff0c\u518d\u91ca\u653e\u5185\u5b58\u7a7a\u95f4\uff0c\u6548\u7387\u975e\u5e38\u4f4e\uff0c\u4f60\u4e5f\u8bb8\u64cd\u4f5c\u6bd4\u8f83\u5c11\u7684\u6570\u636e\u7684\u65f6\u5019\u770b\u4e0d\u51fa\u6765\uff0c\u611f\u89c9\u5f71\u54cd\u4e0d\u5927\uff0c\u4f46\u662f\u4f60\u78b0\u5230\u64cd\u4f5c\u6570\u636e\u91cf\u6bd4\u8f83\u591a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u8981\u9000\u4f11\u4e86\u3002<\/p>\n<h6>\u65b9\u6cd52\uff1ajoin\u65b9\u6cd5<\/h6>\n<pre class=\"lang:default decode:true\">list_of_strings = ['abc', 'def', 'ghi']\nfoobar = ''.join(list_of_strings)<\/pre>\n<h6>\u65b9\u6cd53\uff1a\u66ff\u6362<\/h6>\n<pre class=\"lang:default decode:true\">foobar = '%s, %s' % ('abc', 'def')\nfoobar = '%s%s' % ('abc', 'def')<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.cnpythoner.com\/post\/67.html\" target=\"_blank\">python\u5b57\u7b26\u4e32\u8fde\u63a5<\/a><\/li>\n<li><a href=\"http:\/\/outofmemory.cn\/code-snippet\/1309\/python-changjian-charaeter-chuan-operation\" target=\"_blank\">python\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python\u751f\u6210GUID &gt;&gt;&gt; import uuid &gt;&gt;&gt; uuid [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[8,19],"class_list":["post-1488","post","type-post","status-publish","format-standard","hentry","category-other","category-programing","tag-python","tag-tips"],"views":4031,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1488","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=1488"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1488\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}