{"id":4413,"date":"2019-05-12T06:40:05","date_gmt":"2019-05-11T22:40:05","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4413"},"modified":"2019-06-16T21:56:57","modified_gmt":"2019-06-16T13:56:57","slug":"python-2-%e5%92%8c-3-%e7%9a%84%e6%ad%a3%e5%88%99%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4413.html","title":{"rendered":"Python 2 \u548c 3 \u7684\u6b63\u5219\u5339\u914d"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u8fd9\u4e2a\u6807\u9898\u5176\u5b9e\u8d77\u7684\u4e0d\u592a\u51c6\u786e\uff0c\u56e0\u4e3aPython 2 \u548cPython 3\u4e2d\u7684\u5b98\u65b9\u6b63\u5219\u6a21\u5757re\u5728\u4e0d\u540c\u7248\u672c\u4e2d\u5e76\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\uff0c\u90fd\u652f\u6301\u7684\u662fPerl\u98ce\u683c\u7684\u6b63\u5219\u8bed\u6cd5\uff0c\u552f\u4e00\u7684\u4e0d\u540c\u53ef\u80fd\u662f\u5728Python 3\u4e2d\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\uff0c\u56e0\u6b64\u4e0d\u4f1a\u5b58\u5728\u7f16\u7801\u65b9\u9762\u7684\u95ee\u9898\uff0c\u800cPython 2\u5219\u53ef\u80fd\u4f1a\u6709\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u60f3\u8bb0\u5f55\u5c31\u662f\u6700\u8fd1\u78b0\u5230\u7684\u4e00\u4e2a\u6bd4\u8f83\u6709\u610f\u601d\u7684\u6b63\u5219\u5339\u914d\u7684case\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u5f53\u524d\u5e0c\u671b\u7528Python\u5c06\u6587\u6863\u4e2d\u5305\u542b\u300c\u5bc6\u7801\uff1a123456\u300d\u8fd9\u79cd\u6837\u5f0f\u7684\u5185\u5bb9\u7528\u300cxxx\u300d\u8fdb\u884c\u6279\u91cf\u66ff\u6362\uff0c\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>data = \"\"\"\u6b64\u5904\u5b58\u653e\u5bc6\u7801\uff1aabcdefg\u3002\n\n=\n\u6b64\u5904\u5b58\u653e\u5bc6\u7801\uff1a112abcdef\u3002\n\n\u6700\u540e\u4e00\u884c\n\"\"\"\n\n# \u671f\u671b\u8fbe\u5230\u7684\u66ff\u6362\u6548\u679c\u5982\u4e0b\uff1a\ndata_new = \"\"\"\u6b64\u5904\u5b58\u653exxx\u3002\n\n=\n\u6b64\u5904\u5b58\u653exxx\u3002\n\n\u6700\u540e\u4e00\u884c\n\"\"\"<\/code><\/pre>\n\n\n\n<p>\u5bf9\u5e94\u7684Python\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import re\n\ndata = \"\"\"\u6b64\u5904\u5b58\u653e\u5bc6\u7801\uff1aabcdefg\u3002\n\n=\n\u6b64\u5904\u5b58\u653e\u5bc6\u7801\uff1a112abcdef\u3002\n\n\u6700\u540e\u4e00\u884c\n\"\"\"\n\nprint data\n\n# Python 3\ndata_new = re.sub(r'\u5bc6\u7801[\uff1a:=]{1}(&lt;[^>]+>)*([a-zA-Z0-9\\.\\#\\?\\$\\_\\-\\[\\]\\@\\\/\\*\\)]+)', 'xxx', data)\nprint(data_new)\n\n# Python 2\/3\ndata_new = re.sub(r'\u5bc6\u7801(\uff1a|:|=){1}(&lt;[^>]+>)*([a-zA-Z0-9\\.\\#\\?\\$\\_\\-\\[\\]\\@\\\/\\*\\)]+)', 'xxx', data)\nprint(data_new)\n\n# Python 2\ndata_new = re.sub(u'\u5bc6\u7801[\uff1a:=]{1}(&lt;[^>]+>)*([a-zA-Z0-9\\.\\#\\?\\$\\_\\-\\[\\]\\@\\\/\\*\\)]+)', 'xxx', data.decode('utf8'))\nprint(data_new)\n# Python 2\ndata_new = re.sub(u'\u5bc6\u7801[\uff1a:=]{1}(&lt;[^>]{1,}>){0,}([a-zA-Z0-9\\.\\#\\?\\$\\_\\-\\[\\]\\@\\\/\\*\\)]{1,})', 'xxx', data.decode('utf8'))\nprint(data_new)<\/code><\/pre>\n\n\n\n<p>\u7ecf\u8fc7\u6d4b\u8bd5\u548c\u5bf9\u6bd4\u53d1\u73b0\uff0c\u8fd9\u91cc\u7684\u4e0d\u540c\u4e4b\u5904\u5c31\u5728\u4e8e\u5728Python 2\u548cPython 3\u4e2d\u5747\u53ef\u4ee5\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>(\uff1a|:|=)<\/code><\/pre>\n\n\n\n<p>\u8868\u793a\u300c\u4e2d\u6587\u5192\u53f7\/\u82f1\u6587\u5192\u53f7\/\u82f1\u6587\u7b49\u4e8e\u53f7\u300d\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5bf9\u4e8e\uff08raw\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>[\uff1a:=]<\/code><\/pre>\n\n\n\n<p>\u53ea\u5728Python 3\u4e2d\u53ef\u884c\uff0c\u5728Python 2\u4e2d\u8fdb\u884cunicode\u5339\u914d\u8981\u5148\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u663e\u793a\u8f6c\u4e3autf8\uff0c\u5426\u5219\u8fbe\u4e0d\u5230\u9884\u671f\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u6700\u8fd1\u78b0\u5230\u7684\u4e00\u4e2a\u6bd4\u8f83\u6709\u610f\u601d\u7684case\uff0c\u5dee\u70b9\u5c31\u4e2d\u62db\u4e86\u3002\u3002\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/liangchao.wordpress.com\/2012\/10\/04\/python%E4%B8%8B%E4%BD%BF%E7%94%A8%E4%B8%AD%E6%96%87%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F\/\">Python\u4e0b\u4f7f\u7528\u4e2d\u6587\u6b63\u5219\u8868\u8fbe\u5f0f<\/a><\/li><li><a href=\"https:\/\/www.evernote.com\/shard\/s229\/sh\/0490e5c6-020f-4edc-9b35-1a4dcf1bef2d\/192c189e398edf9a8c8f7abce1ac940a\">python\u6b63\u5219\u5339\u914d\u4e2d\u6587<\/a><\/li><li><a href=\"https:\/\/www.runoob.com\/python\/python-2x-3x.html\">Python2.x\u4e0e3\u200b\u200b.x\u7248\u672c\u533a\u522b<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/2\/howto\/regex.html\">https:\/\/docs.python.org\/2\/howto\/regex.html<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/howto\/regex.html\">https:\/\/docs.python.org\/3\/howto\/regex.html<\/a><\/li><li><a href=\"https:\/\/wiki.python.org\/moin\/Python2orPython3\/\">https:\/\/wiki.python.org\/moin\/Python2orPython3\/<\/a><\/li><li><a href=\"https:\/\/pypi.org\/project\/regex\/\">https:\/\/pypi.org\/project\/regex\/<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u8fd9\u4e2a\u6807\u9898\u5176\u5b9e\u8d77\u7684\u4e0d\u592a\u51c6\u786e\uff0c\u56e0\u4e3aPython 2 \u548cPython 3\u4e2d\u7684\u5b98\u65b9\u6b63\u5219\u6a21\u5757re [&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":[8,393,1382,217,255],"class_list":["post-4413","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python","tag-re","tag-re-sub","tag-unicode","tag-utf-8"],"views":4333,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4413","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=4413"}],"version-history":[{"count":3,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4413\/revisions"}],"predecessor-version":[{"id":4489,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4413\/revisions\/4489"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}