{"id":1133265,"date":"2025-01-08T21:04:51","date_gmt":"2025-01-08T13:04:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133265.html"},"modified":"2025-01-08T21:04:53","modified_gmt":"2025-01-08T13:04:53","slug":"python%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133265.html","title":{"rendered":"Python\u4e8c\u8fdb\u5236\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102543\/a02b6c51-ca54-41ad-8d39-381d8038d578.webp\" alt=\"Python\u4e8c\u8fdb\u5236\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u4e8c\u8fdb\u5236\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/strong> \u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5b57\u7b26\uff0c\u6216\u8005\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u8fdb\u884c\u590d\u6742\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001replace() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>replace()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b50\u5b57\u7b26\u4e32\u3002\u5728\u9700\u8981\u5220\u9664\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;hello, binary world!&quot;<\/p>\n<p>result_string = original_string.replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>print(result_string)  # \u8f93\u51fa: &quot;hello,  world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684 &quot;binary&quot; \u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u53bb\u9664\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001strip() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>strip()<\/code>\u65b9\u6cd5\u7528\u4e8e\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u6216\u6307\u5b9a\u5b57\u7b26\u3002<code>lstrip()<\/code>\u548c<code>rstrip()<\/code>\u5206\u522b\u7528\u4e8e\u53bb\u9664\u5de6\u4fa7\u548c\u53f3\u4fa7\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;    hello world!    &quot;<\/p>\n<p>result_string = original_string.strip()<\/p>\n<p>print(result_string)  # \u8f93\u51fa: &quot;hello world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u53bb\u9664\u4e86\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002Python \u7684 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h3>1\u3001re.sub() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>re.sub()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_string = &quot;hello, binary world!&quot;<\/p>\n<p>pattern = r&quot;binary\\s&quot;<\/p>\n<p>result_string = re.sub(pattern, &quot;&quot;, original_string)<\/p>\n<p>print(result_string)  # \u8f93\u51fa: &quot;hello, world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f <code>r&quot;binary\\s&quot;<\/code> \u5339\u914d &quot;binary &quot;\uff08\u6ce8\u610f\u7a7a\u683c\uff09\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001re.findall() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5148\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u518d\u8fdb\u884c\u5904\u7406\u3002<code>re.findall()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_string = &quot;hello, binary world! binary data&quot;<\/p>\n<p>pattern = r&quot;binary\\s&quot;<\/p>\n<p>matches = re.findall(pattern, original_string)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;binary &#39;, &#39;binary &#39;]<\/p>\n<p>for match in matches:<\/p>\n<p>    original_string = original_string.replace(match, &quot;&quot;)<\/p>\n<p>print(original_string)  # \u8f93\u51fa: &quot;hello, world! data&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5148\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u9010\u4e00\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u7f16\u7801\u6709 UTF-8\u3001UTF-16 \u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u7f16\u7801\u548c\u89e3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;hello, binary world!&quot;<\/p>\n<p>binary_data = original_string.encode(&#39;utf-8&#39;)<\/p>\n<p>print(binary_data)  # \u8f93\u51fa: b&#39;hello, binary world!&#39;<\/p>\n<h2><strong>\u89e3\u7801\u65f6\u53bb\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = binary_data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>print(decoded_string)  # \u8f93\u51fa: &quot;hello,  world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5148\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u7136\u540e\u5728\u89e3\u7801\u65f6\u53bb\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u975eASCII\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u7f16\u7801\u548c\u89e3\u7801\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;hello, \u4e16\u754c!&quot;<\/p>\n<p>binary_data = original_string.encode(&#39;utf-8&#39;)<\/p>\n<p>print(binary_data)  # \u8f93\u51fa: b&#39;hello, \\xe4\\xb8\\x96\\xe7\\x95\\x8c!&#39;<\/p>\n<h2><strong>\u89e3\u7801\u65f6\u53bb\u9664\u7279\u5b9a\u975eASCII\u5b57\u7b26<\/strong><\/h2>\n<p>decoded_string = binary_data.decode(&#39;utf-8&#39;).replace(&quot;\u4e16\u754c&quot;, &quot;&quot;)<\/p>\n<p>print(decoded_string)  # \u8f93\u51fa: &quot;hello, !&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5904\u7406\u4e86\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u89e3\u7801\u65f6\u53bb\u9664\u4e86\u7279\u5b9a\u975eASCII\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u627e\u5230\u7279\u5b9a\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_string = &quot;hello, binary world! binary data&quot;<\/p>\n<p>pattern = r&quot;binary\\s&quot;<\/p>\n<p>matches = re.findall(pattern, original_string)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;binary &#39;, &#39;binary &#39;]<\/p>\n<p>for match in matches:<\/p>\n<p>    original_string = original_string.replace(match, &quot;&quot;)<\/p>\n<p>print(original_string)  # \u8f93\u51fa: &quot;hello, world! data&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u9010\u4e00\u8fdb\u884c\u66ff\u6362\uff0c\u6700\u7ec8\u5b9e\u73b0\u4e86\u53bb\u9664\u5b57\u7b26\u4e32\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u5b57\u7b26\u4e32\u5904\u7406\u6765\u5b9e\u73b0\u53bb\u9664\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;binary_file.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    binary_data = file.read()<\/p>\n<p>print(binary_data)  # \u8f93\u51fa: \u6587\u4ef6\u7684\u4e8c\u8fdb\u5236\u5185\u5bb9<\/p>\n<h2><strong>\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u5e76\u53bb\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = binary_data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>print(decoded_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5728\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u65f6\u53bb\u9664\u4e86\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;binary_file.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    binary_data = file.read()<\/p>\n<h2><strong>\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u5e76\u53bb\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>decoded_string = binary_data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>new_binary_data = decoded_string.encode(&#39;utf-8&#39;)<\/p>\n<h2><strong>\u5199\u5165\u65b0\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;new_binary_file.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(new_binary_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u53bb\u9664\u4e86\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5904\u7406\u540e\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d41\u5f0f\u8bfb\u53d6\u548c\u5904\u7406\u7684\u65b9\u5f0f\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6d41\u5f0f\u8bfb\u53d6\u548c\u5904\u7406<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_binary_file.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(1024)  # \u6bcf\u6b21\u8bfb\u53d6 1024 \u5b57\u8282<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        decoded_chunk = chunk.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>        new_binary_data = decoded_chunk.encode(&#39;utf-8&#39;)<\/p>\n<p>        with open(&#39;new_large_binary_file.bin&#39;, &#39;ab&#39;) as new_file:<\/p>\n<p>            new_file.write(new_binary_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4ee5\u6d41\u5f0f\u65b9\u5f0f\u8bfb\u53d6\u548c\u5904\u7406\u5927\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u907f\u514d\u4e86\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5bfc\u81f4\u7684\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u7f51\u7edc\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u63a5\u6536\u7f51\u7edc\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a TCP\/IP \u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u670d\u52a1\u5668<\/strong><\/h2>\n<p>server_address = (&#39;localhost&#39;, 10000)<\/p>\n<p>sock.connect(server_address)<\/p>\n<p>try:<\/p>\n<p>    # \u53d1\u9001\u6570\u636e<\/p>\n<p>    message = &#39;hello, binary world!&#39;<\/p>\n<p>    sock.sendall(message.encode(&#39;utf-8&#39;))<\/p>\n<p>    # \u63a5\u6536\u6570\u636e<\/p>\n<p>    data = sock.recv(1024)<\/p>\n<p>    print(&#39;received:&#39;, data.decode(&#39;utf-8&#39;))<\/p>\n<p>    # \u5904\u7406\u63a5\u6536\u7684\u6570\u636e<\/p>\n<p>    processed_data = data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;).encode(&#39;utf-8&#39;)<\/p>\n<p>    print(&#39;processed:&#39;, processed_data.decode(&#39;utf-8&#39;))<\/p>\n<p>finally:<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a TCP\/IP \u5957\u63a5\u5b57\uff0c\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\uff0c\u5e76\u5728\u63a5\u6536\u540e\u5904\u7406\u6570\u636e\u4ee5\u53bb\u9664\u7279\u5b9a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u53d1\u9001\u5904\u7406\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a TCP\/IP \u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8fde\u63a5\u670d\u52a1\u5668<\/strong><\/h2>\n<p>server_address = (&#39;localhost&#39;, 10000)<\/p>\n<p>sock.connect(server_address)<\/p>\n<p>try:<\/p>\n<p>    # \u53d1\u9001\u6570\u636e<\/p>\n<p>    message = &#39;hello, binary world!&#39;<\/p>\n<p>    sock.sendall(message.encode(&#39;utf-8&#39;))<\/p>\n<p>    # \u63a5\u6536\u6570\u636e<\/p>\n<p>    data = sock.recv(1024)<\/p>\n<p>    print(&#39;received:&#39;, data.decode(&#39;utf-8&#39;))<\/p>\n<p>    # \u5904\u7406\u63a5\u6536\u7684\u6570\u636e<\/p>\n<p>    processed_data = data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;).encode(&#39;utf-8&#39;)<\/p>\n<p>    # \u53d1\u9001\u5904\u7406\u540e\u7684\u6570\u636e<\/p>\n<p>    sock.sendall(processed_data)<\/p>\n<p>    print(&#39;sent processed data:&#39;, processed_data.decode(&#39;utf-8&#39;))<\/p>\n<p>finally:<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u63a5\u6536\u6570\u636e\u540e\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u53d1\u9001\u56de\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f18\u5316\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u6216\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    decoded_chunk = chunk.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;)<\/p>\n<p>    return decoded_chunk.encode(&#39;utf-8&#39;)<\/p>\n<p>with open(&#39;large_binary_file.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(1024)  # \u6bcf\u6b21\u8bfb\u53d6 1024 \u5b57\u8282<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        thread = threading.Thread(target=process_chunk, args=(chunk,))<\/p>\n<p>        thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406\u5927\u6587\u4ef6\u7684\u6bcf\u4e2a\u5757\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5f02\u6b65IO<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f51\u7edc\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65IO\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def handle_connection(reader, writer):<\/p>\n<p>    data = aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t reader.read(1024)<\/p>\n<p>    message = data.decode(&#39;utf-8&#39;).replace(&quot;binary&quot;, &quot;&quot;).encode(&#39;utf-8&#39;)<\/p>\n<p>    writer.write(message)<\/p>\n<p>    await writer.drain()<\/p>\n<p>    writer.close()<\/p>\n<p>async def main():<\/p>\n<p>    server = await asyncio.start_server(handle_connection, &#39;127.0.0.1&#39;, 8888)<\/p>\n<p>    async with server:<\/p>\n<p>        await server.serve_forever()<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f02\u6b65IO\u5904\u7406\u7f51\u7edc\u8fde\u63a5\u548c\u6570\u636e\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u548c\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u6027\u80fd\u4f18\u5316\u6280\u5de7\u6765\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5c06Python\u4e2d\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u666e\u901a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>chr()<\/code>\u51fd\u6570\u5c06\u8be5\u6574\u6570\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u82e5\u4f60\u6709\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<code>&#39;1100001&#39;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a  <\/p>\n<pre><code class=\"language-python\">binary_str = &#39;1100001&#39;\nchar = chr(int(binary_str, 2))\nprint(char)  # \u8f93\u51fa: a\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5c06\u4efb\u4f55\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728Python\u4e2d\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u7684\u524d\u7f00<code>0b<\/code>\u53bb\u6389\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u65f6\u53d1\u73b0\u5b83\u5e26\u6709\u524d\u7f00<code>0b<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u7684\u65b9\u6cd5\u53bb\u6389\u8fd9\u4e2a\u524d\u7f00\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">binary_str = &#39;0b1100001&#39;\ncleaned_str = binary_str[2:]  # \u53bb\u6389\u524d\u7f00\nprint(cleaned_str)  # \u8f93\u51fa: 1100001\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u53ef\u4ee5\u5e94\u7528\u4e8e\u4efb\u4f55\u5e26\u6709<code>0b<\/code>\u524d\u7f00\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u4e2d\u53bb\u6389\u67d0\u4e9b\u7279\u5b9a\u7684\u5b57\u7b26\uff08\u4f8b\u5982<code>1<\/code>\u6216<code>0<\/code>\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">binary_str = &#39;1101001&#39;\nmodified_str = binary_str.replace(&#39;1&#39;, &#39;&#39;)  # \u53bb\u6389\u6240\u6709\u7684&#39;1&#39;\nprint(modified_str)  # \u8f93\u51fa: 00\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u7075\u6d3b\u5730\u5904\u7406\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u53bb\u9664\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e8c\u8fdb\u5236\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u548c\u89e3\u7801\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5728\u5177\u4f53\u5e94 [&hellip;]","protected":false},"author":3,"featured_media":1133274,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133265"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1133265"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133265\/revisions"}],"predecessor-version":[{"id":1133275,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133265\/revisions\/1133275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}