{"id":2456,"date":"2015-08-02T21:58:51","date_gmt":"2015-08-02T13:58:51","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2456"},"modified":"2015-08-02T21:58:51","modified_gmt":"2015-08-02T13:58:51","slug":"python2%e4%b8%ad%e7%9a%84string-maketrans","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2456.html","title":{"rendered":"Python2\u4e2d\u7684string.maketrans"},"content":{"rendered":"<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>python string.maketrans<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/string.html#string.maketrans\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/string.html#string.maketrans<\/a><\/li>\n<li><a href=\"http:\/\/pymotw.com\/2\/string\/\" target=\"_blank\">http:\/\/pymotw.com\/2\/string\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/15815696\/i-need-to-know-exactly-how-to-use-string-maketrans\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/15815696\/i-need-to-know-exactly-how-to-use-string-maketrans<\/a><\/li>\n<li><a href=\"http:\/\/gis.stackexchange.com\/questions\/29694\/how-to-implent-a-maketrans-python-function-on-field-calculator\" target=\"_blank\">http:\/\/gis.stackexchange.com\/questions\/29694\/how-to-implent-a-maketrans-python-function-on-field-calculator<\/a><\/li>\n<li><a href=\"http:\/\/nullege.com\/codes\/search\/string.maketrans\" target=\"_blank\">http:\/\/nullege.com\/codes\/search\/string.maketrans<\/a><\/li>\n<li><a href=\"http:\/\/wangwei007.blog.51cto.com\/68019\/1242206\" target=\"_blank\">http:\/\/wangwei007.blog.51cto.com\/68019\/1242206<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>Python\u4e2d\u7684 string.maketrans(from, to) \u7528\u4e8e\u5c06\u53c2\u6570\u4e2d\u7684 from \u6620\u5c04\u6210 to (\u5bf9\u4f4d\u6620\u5c04\uff0c\u6240\u4ee5\u9700\u8981&#8217;from&#8217;\u548c&#8217;to&#8217;\u7684\u957f\u5ea6\u4e00\u81f4)\uff0c\u7136\u540e\u4f20\u9012\u7ed9 string.translate() \u8fdb\u884c\u5904\u7406\u3002<span style=\"color: #ff0000;\"><strong>\u7c7b\u4f3c\u4e8eLinux\u7684 tr \u547d\u4ee4<\/strong><\/span>(tr &#8216;set1&#8217; &#8216;set2&#8217;)\u3002<\/p>\n<p>\u4e0d\u8981\u4f7f\u7528 lowercase \u548c uppercase \u4f5c\u4e3a string.maketrans() \u7684\u53c2\u6570\uff0c\u56e0\u4e3a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5b83\u4eec\u7684\u957f\u5ea6\u662f\u4e0d\u4e00\u81f4\u7684\u3002<strong><span style=\"color: #0000ff;\">\u5982\u679c\u8981\u8fdb\u884c\u5927\u5c0f\u5199\u7684\u8f6c\u6362\uff0c\u8bf7\u4f7f\u7528 str.lower() \u548c str.upper() \u65b9\u6cd5<\/span><\/strong>\u3002<\/p>\n<pre class=\"lang:default decode:true\">astr = 'How can you tell an extrovert from an introvert at NSA?'\n\nimport string\nrot13 = string.maketrans(\n\t\t\tu\"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz\",\n\t\t\tu\"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm\"\n\t\t)\nprint astr.translate(rot13)\n\nprint astr.encode('rot13')\n\nimport codecs\nprint codecs.encode(astr, 'rot_13')<\/pre>\n<h6>\u6ce8\u610f\uff1a<\/h6>\n<p>string.translate() \u5728 Python3 \u4e2d\u5df2\u7ecf<a href=\"https:\/\/docs.python.org\/2\/library\/string.html#deprecated-string-functions\" target=\"_blank\">\u88ab\u5e9f\u5f03\u4e86<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u5173\u952e\u5b57\uff1a python string.maketrans \u53c2\u8003\u94fe\u63a5\uff1a https:\/\/docs.pytho [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[8,558],"class_list":["post-2456","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python","tag-string"],"views":2431,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2456","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=2456"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2456\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}