{"id":33,"date":"2026-03-11T15:45:22","date_gmt":"2026-03-11T07:45:22","guid":{"rendered":"https:\/\/www.loongdi.net\/blog\/?p=33"},"modified":"2026-03-11T15:45:22","modified_gmt":"2026-03-11T07:45:22","slug":"python-%e4%b8%ad%e7%9a%84%e5%8d%81%e5%85%ad%e8%bf%9b%e5%88%b6%e5%8c%b9%e9%85%8d%e6%8a%80%e6%9c%af%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.loongdi.net\/blog\/33.html","title":{"rendered":"Python \u4e2d\u7684\u5341\u516d\u8fdb\u5236\u5339\u914d\u6280\u672f\u8be6\u89e3"},"content":{"rendered":"<p><h2>Python 16\u8fdb\u5236\u5339\u914d\u6280\u672f\u6982\u8ff0<\/h2>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c16\u8fdb\u5236\uff08\u5341\u516d\u8fdb\u5236\uff09\u6570\u636e\u5339\u914d\u662f\u4e00\u9879\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7279\u522b\u5728\u6570\u636e\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u548c\u5b89\u5168\u6027\u5206\u6790\u7b49\u9886\u57df\u3002\u672c\u6587\u7684\u4efb\u52a1\u662f\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5341\u516d\u8fdb\u5236\u5339\u914d\uff0c\u5305\u62ec\u6b65\u9aa4\u3001\u547d\u4ee4\u793a\u4f8b\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u597d\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6807\u51c6\u5e93\u548c\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff08\u5982re\uff09\u6765\u5b9e\u73b016\u8fdb\u5236\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u73af\u5883\u642d\u5efa<\/h3>\n<\/p>\n<ul>\n<li>\u5b89\u88c5Python: \u53ef\u4ee5\u4ece\u5b98\u7f51\uff08https:\/\/www.python.org\/downloads\/\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Python\u3002<\/li>\n<li>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff08\u5982\u6709\u9700\u8981\uff09: \u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<p><pre><code>pip install re<\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u516d\u8fdb\u5236\u5339\u914d\u57fa\u672c\u6982\u5ff5<\/h2>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u662f\u4ee5\u57fa\u657016\u8868\u793a\u7684\u6570\uff0c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u5e38\u7528\u7684\u4e00\u79cd\u6570\u5236\u3002\u6bcf\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4ee3\u88684\u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0c\u4e00\u4e2a\u5b57\u8282\u53ef\u4ee5\u8868\u793a\u4e3a\u4e24\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u64cd\u4f5c\u6b65\u9aa4<\/h2>\n<\/p>\n<p><h3>1. \u5bfc\u5165\u9700\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code>import re<\/code><\/pre>\n<\/p>\n<p><h3>2. \u5b9a\u4e49\u5341\u516d\u8fdb\u5236\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u6570\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a<b>&#8216;\\b[0-9a-fA-F]+(?:\\s*[0-9a-fA-F]+\\b)*&#8217;<\/b>\uff0c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u4efb\u610f\u957f\u5ea6\u7684\u5341\u516d\u8fdb\u5236\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>3. \u7f16\u5199\u5339\u914d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u7528\u4ee5\u67e5\u627e\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5341\u516d\u8fdb\u5236\u6570\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>def find_hexadecimal(data):<\/p>\r\n<p>    hex_pattern = r'\\b[0-9a-fA-F]+\\b'<\/p>\r\n<p>    return re.findall(hex_pattern, data)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u6d4b\u8bd5\u5339\u914d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u6765\u6d4b\u8bd5\u521a\u521a\u5b9a\u4e49\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>test_data = \"Here are some hex values: 1a3e, 4B2, and A5F.\"<\/p>\r\n<p>matched_hex = find_hexadecimal(test_data)<\/p>\r\n<p>print(matched_hex)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<ul>\n<li><strong>\u5927\u5c0f\u5199\u654f\u611f:<\/strong> \u6b63\u5219\u8868\u8fbe\u5f0f\u533a\u5206\u5927\u5c0f\u5199\uff0c\u6240\u4ee5\u8bf7\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u8303\u56f4\u3002<\/li>\n<li><strong>\u8fb9\u754c\u5339\u914d:<\/strong> \u4f7f\u7528\\b\u53ef\u4ee5\u786e\u4fdd\u5339\u914d\u7684\u5b57\u7b26\u4e32\u662f\u5b8c\u6574\u7684\u5341\u516d\u8fdb\u5236\u6570\u800c\u4e0d\u662f\u5176\u4ed6\u5b57\u7b26\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898:<\/strong> \u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n<p><h2>\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<p><h3>1. \u9ad8\u7ea7\u5339\u914d<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5339\u914d\u5e26\u6709\u524d\u7f00\u7684\u5341\u516d\u8fdb\u5236\u6570\uff0c\u4f8b\u5982 0x\u62160X\uff0c\u53ef\u4ee5\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a:<b>r&#8217;\\b0[xX][0-9a-fA-F]+\\b&#8217;<\/b>\u3002<\/p>\n<\/p>\n<p><h3>2. \u81ea\u5b9a\u4e49\u5339\u914d\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u8981\u81ea\u5b9a\u4e49\u5339\u914d\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5339\u914d\u4ee50x\u5f00\u5934\uff0c\u540e\u9762\u8ddf\u968f\u4e24\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>def find_custom_hexadecimal(data):<\/p>\r\n<p>    custom_hex_pattern = r'\\b0[xX][0-9a-fA-F]{2}\\b'<\/p>\r\n<p>    return re.findall(custom_hex_pattern, data)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5904\u7406\u5339\u914d\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u53ef\u5bf9\u5339\u914d\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u6bd4\u5982\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.loongdi.net\/blog\/wp-content\/uploads\/2026\/03\/8U3JhdO9K.jpg\" alt=\"Python \u4e2d\u7684\u5341\u516d\u8fdb\u5236\u5339\u914d\u6280\u672f\u8be6\u89e3\" title=\"Python \u4e2d\u7684\u5341\u516d\u8fdb\u5236\u5339\u914d\u6280\u672f\u8be6\u89e3\"><\/p>\n<p><pre><code><\/p>\r\n<p>def hex_to_int(hex_list):<\/p>\r\n<p>    return [int(h, 16) for h in hex_list]<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u793a\u4f8b\u573a\u666f\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1. \u4ece\u5927\u6587\u4ef6\u4e2d\u63d0\u53d6\u5341\u516d\u8fdb\u5236\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u7684\u5339\u914d\u51fd\u6570\u63d0\u53d6\u5341\u516d\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>with open('large_file.txt', 'r') as file:<\/p>\r\n<p>    for line in file:<\/p>\r\n<p>        matched_hex = find_hexadecimal(line)<\/p>\r\n<p>        # \u5904\u7406\u5339\u914d\u7ed3\u679c\uff0c\u4f8b\u5982\u5b58\u50a8\u6216\u8ba1\u7b97<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7f51\u7edc\u6570\u636e\u5305\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\u5206\u6790\u6293\u5305\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u63d0\u53d6\u5341\u516d\u8fdb\u5236\u6570\u636e\uff0c\u4f7f\u7528\u76f8\u4f3c\u7684\u65b9\u6cd5\u53ef\u5b9e\u73b0\u81ea\u52a8\u5316\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code><\/p>\r\n<p>data_packet = \"GET \/ HTTP\/1.1\\r\\nHost: example.com\\r\\nContent-Length: 19\\r\\n\\r\\n0x1a2b3c4d\"<\/p>\r\n<p>matched_hex = find_hexadecimal(data_packet)<\/p>\r\n<p># \u8fdb\u4e00\u6b65\u5904\u7406<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5341\u516d\u8fdb\u5236\u5339\u914d\u5728Python\u4e2d\u662f\u4e00\u4e2a\u57fa\u672c\u4f46\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\u3002\u638c\u63e1\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u5728\u5341\u516d\u8fdb\u5236\u5339\u914d\u4e2d\u7684\u5e94\u7528\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u5404\u7c7b\u6570\u636e\u5904\u7406\u4e2d\u53d1\u6325\u5de8\u5927\u7684\u4f5c\u7528\u3002\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u6b65\u9aa4\u548c\u63d0\u793a\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u8fdb\u884c\u5341\u516d\u8fdb\u5236\u6570\u636e\u5339\u914d\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 16\u8fdb\u5236\u5339\u914d\u6280\u672f\u6982\u8ff0 \u5728Python\u7f16\u7a0b\u4e2d\uff0c16\u8fdb\u5236\uff08\u5341\u516d\u8fdb\u5236\uff09\u6570\u636e\u5339\u914d\u662f\u4e00\u9879\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7279\u522b\u5728\u6570\u636e\u5904\u7406\u3001\u7f51\u7edc\u7f16\u7a0b\u548c\u5b89\u5168\u6027\u5206\u6790\u7b49\u9886\u57df\u3002\u672c\u6587\u7684\u4efb\u52a1\u662f\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5341\u516d\u8fdb\u5236\u5339\u914d\uff0c\u5305\u62ec\u6b65\u9aa4\u3001\u547d\u4ee4\u793a\u4f8b\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u5b9e\u7528&#8230;<\/p>\n","protected":false},"author":1,"featured_media":34,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-33","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-yun"],"_links":{"self":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions\/35"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media\/34"}],"wp:attachment":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/tags?post=33"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/topic?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}