{"id":519,"date":"2023-09-16T15:39:10","date_gmt":"2023-09-16T07:39:10","guid":{"rendered":""},"modified":"2023-09-17T00:42:13","modified_gmt":"2023-09-16T16:42:13","slug":"python-%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e5%8f%8d%e6%96%9c%e6%9d%a0","status":"publish","type":"post","link":"https:\/\/mushiming.com\/519.html","title":{"rendered":"python \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u53cd\u659c\u6760 \\"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

python \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u53cd\u659c\u6760 \\<\/h2>\n

\u9047\u5230\u7684\u95ee\u9898\uff1a<\/h3>\n

\u5728\u505a\u722c\u866b\u7684\u65f6\u5019\uff0c\u722c\u53d6\u7684url\u94fe\u63a5\u5185\u8fd8\u6709\u8f6c\u4e49\u5b57\u7b26\uff0c\u53cd\u659c\u6760 \\<\/code>\uff0c\u6253\u7b97\u7528\u6b63\u5219\u7684re.sub()\u66ff\u6362\u6389\u7684\u65f6\u5019\u9047\u5230\u4e86\u95ee\u9898\uff0c
\u8fd9\u662f\u8981\u505a\u66ff\u6362\u7684\u5b57\u7b26\u4e32
\"python<\/p>\n

\u6700\u5f00\u59cb\u76f4\u63a5\u5199<\/p>\n

re.<\/span>sub(<\/span>\"\\\\\"<\/span>,<\/span>\"\"<\/span>,<\/span>item)<\/span>\n<\/code><\/pre>\n

\u7f16\u8bd1\u5668\u6f0f\u7ea2\u4e86
\"python
\u7136\u540e\u5c31\u662f\u627e\u89e3\u51b3\u529e\u6cd5\uff0c\u6700\u540e\u53d1\u73b0\u8981\u7528\u56db\u4e2a\u53cd\u659c\u6760\u624d\u53ef\u4ee5\uff0c\u4e5f\u5c31\u662f\u4f7f\u7528<\/p>\n

re.<\/span>sub(<\/span>\"\\\\\\\\\"<\/span>,<\/span>\"\"<\/span>,<\/span>item)<\/span>\n<\/code><\/pre>\n

\u67e5\u4e86\u67e5\u8d44\u6599\uff0c\u7b80\u5355\u8bf4\u8bf4\u6211\u81ea\u5df1\u7684\u7406\u89e3\u3002<\/p>\n

\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n

\u9996\u5148\u5c31\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bf4\uff0c\u4ed6\u7684\u8bed\u6cd5\u662f\u72ec\u7acb\u7684\uff0c\u6709\u81ea\u5df1\u7684\u8bed\u6cd5\uff0c\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u7531\u4e8e\u53cd\u659c\u6760 \\<\/code> \u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u53ef\u4ee5\u548c\u5176\u4ed6\u5b57\u6bcd\u5f62\u6210\u8f6c\u4e49\u5b57\u7b26\uff0c\u6240\u4ee5\u8981\u60f3\u8868\u793a\u4e00\u4e2a\u53cd\u659c\u6760 \\<\/code> \u5c31\u5fc5\u987b\u5199\u6210 \\\\<\/code> \u8fd9\u79cd\u5f62\u5f0f\u3002\u6240\u4ee5\u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bf4\uff0c\u5982\u679c\u8981\u5339\u914d\u4e00\u4e2a\\<\/code> \u5c31\u8981\u5199\u6210\\\\<\/code>\uff0c\u50cf\u8fd9\u6837\uff1a
\"python<\/p>\n

python\u5b57\u7b26\u4e32<\/h3>\n

\u5728python\u4e2d\uff0c\u5982\u679c\u60f3\u8981\u8f93\u51fa\u4e00\u4e2a\u53cd\u659c\u6760\\<\/code>\u5b57\u7b26\uff0c\u540c\u6837\u8981\u4f7f\u7528\u8f6c\u4e49\uff1a<\/p>\n

>><\/span>><\/span> print<\/span>(<\/span>\"\\\\\"<\/span>)<\/span>\n\\\n<\/code><\/pre>\n

\u540c\u6837\u662f\u56e0\u4e3a\u5728python\u4e2d\u53cd\u659c\u6760\u4e5f\u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n

\u7efc\u4e0a<\/h3>\n

\u5f53\u5199\u6210<\/p>\n

    item =<\/span> \"https:\\\/\\\/jobs.51job.com\/guangzhou-thq\\\/137115906.html?s=sou_sou_soulb&t=0_0\"<\/span>\n    item =<\/span> re.<\/span>sub(<\/span>\"\\\\\\\\\"<\/span>,<\/span>\"\"<\/span>,<\/span>item)<\/span>\n<\/code><\/pre>\n

\u9996\u5148\u4f20\u5165\u7684\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5python\u4e2d\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u5668\u4f1a\u628a\"\\\\\\\\\"<\/code>\u89e3\u6790\u6210
\\\\<\/code>\uff0c\u89e3\u6790\u4e4b\u540e\u4f1a\u518d\u4f20\u9012\u7ed9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u89e3\u6790\u5668\u3002\u7531\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u6709\u81ea\u5df1\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u6240\u4ee5\u5f53\u5b83\u770b\u5230\\\\<\/code>\u65f6\uff0c\u4f1a\u628a\u5b83\u89e3\u6790\u4e3a\u4e00\u4e2a\\<\/code>\uff0c\u6240\u4ee5\u8fd9\u65f6\u5019\u6b63\u5219\u5339\u914d\u5c31\u4f1a\u53ea\u5339\u914d\u4e00\u4e2a\\<\/code>\u3002
\u8d34\u4e00\u4e2aStackoverflow\u4e0a\u7684\u56de\u7b54:<\/p>\n

\n

If you\u2019re putting this in a string within a program, you may actually
need to use four backslashes (because the string parser will remove
two of them when \u201cde-escaping\u201d it for the string, and then the regex
needs two for an escaped regex backslash).
For instance:<\/p>\n

regex(\"\\\\\\\\\")<\/code> is interpreted as\u2026<\/p>\n

regex(\"\\\\\" [escaped backslash] followed by \"\\\\\" [escaped backslash])<\/code>
is interpreted as\u2026<\/p>\n

regex(\\\\)<\/code> is interpreted as a regex that matches a single backslash.<\/p>\n<\/blockquote>\n

\u539f\u6587\u5730\u5740\uff1aCan\u2019t escape the backslash with regex?<\/p>\n

\u5f53\u7136\u8fd8\u53ef\u4ee5\u4f7f\u7528 raw string\u6765\u5199\uff0c\u4e5f\u5c31\u662f\u5199\u6210<\/p>\n

re.<\/span>sub(<\/span>r'\\\\'<\/span>,<\/span>''<\/span>,<\/span>item)<\/span>\n<\/code><\/pre>\n

\u7531\u4e8e\u4f7f\u7528\u4e86r'\\\\'<\/code>\uff0cpython\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u5668\u770b\u5230r'\\\\'<\/code>\u4e4b\u540e\uff0c\u5c31\u76f4\u63a5\u5c06\u5916\u5c42\u7684r''<\/code>\u53bb\u6389\u7136\u540e\u4f20\u9012\u7ed9re\u89e3\u6790\u5668\uff0cre\u89e3\u6790\u5668\u4f1a\u518d\u6b21\u89e3\u6790\\\\<\/code>\u4e3a\\<\/code>\uff0c\u5339\u914d\u5185\u5bb9\u662f\u4e00\u4e2a\u53cd\u659c\u6760\\<\/code><\/p>\n

\u5b57\u7b26\u4e32\u65b9\u6cd5replace()<\/h3>\n

\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u66ff\u6362\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace()<\/p>\n

\n

str.replace(old, new[, max])
old \u2013 \u5c06\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\u3002
new \u2013 \u65b0\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u66ff\u6362old\u5b50\u5b57\u7b26\u4e32\u3002
max \u2013 \u53ef\u9009\u5b57\u7b26\u4e32, \u66ff\u6362\u4e0d\u8d85\u8fc7 max \u6b21<\/p>\n<\/blockquote>\n

>><\/span>><\/span> item\n'https:\\\\\/\\\\\/jobs.51job.com\/guangzhou-thq\\\\\/137115906.html?s=sou_sou_soulb&t=0_0'<\/span>\n>><\/span>><\/span> item.<\/span>replace(<\/span>'\\\\'<\/span>,<\/span>''<\/span>)<\/span>\n'https:\/\/jobs.51job.com\/guangzhou-thq\/137115906.html?s=sou_sou_soulb&t=0_0'<\/span>\n>><\/span>><\/span>\n<\/code><\/pre>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0d\u8bba\u662f\u6b63\u5219\u7684re.sub()\u8fd8\u662fstr.replace()\uff0c\u4f7f\u7528\u4e4b\u540e\u90fd\u4e0d\u4f1a\u5bf9\u539f\u59cb\u5b57\u7b26\u4e32\u6539\u53d8\uff1a<\/p>\n

import<\/span> re\n\nurL =<\/span>'https:\\\/\\\/jobs.51job.com\\\/guangzhou-thq\\\/137735415.html?s=sou_sou_soulb&t=0_0'<\/span>\nprint<\/span>(<\/span>urL.<\/span>replace(<\/span>'\\\\'<\/span>,<\/span>''<\/span>)<\/span>)<\/span>\nprint<\/span>(<\/span>urL)<\/span>\n\nprint<\/span>(<\/span>re.<\/span>sub(<\/span>r'\\\\'<\/span>,<\/span>''<\/span>,<\/span>urL)<\/span>)<\/span>\nprint<\/span>(<\/span>urL)<\/span>\n<\/code><\/pre>\n

\"python<\/p>\n","protected":false},"excerpt":{"rendered":"python \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u53cd\u659c\u6760 \\python\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u53cd\u659c\u6760\\\u9047\u5230\u7684\u95ee\u9898\uff1a\u5728\u505a\u722c\u866b\u7684\u65f6\u5019\uff0c\u722c\u53d6\u7684url\u94fe\u63a5\u5185\u8fd8\u6709\u8f6c\u4e49\u5b57...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/519"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=519"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/519\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}