{"id":205333,"date":"2025-05-29T14:40:06","date_gmt":"2025-05-29T06:40:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205333\/"},"modified":"2025-05-29T14:40:06","modified_gmt":"2025-05-29T06:40:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%b8%85%e7%a9%ba%e6%8e%a7%e5%88%b6%e5%8f%b0%e4%b9%8b%e5%89%8d%e7%9a%84%e8%be%93%e5%87%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205333\/","title":{"rendered":"Python\u4e2d\u5982\u4f55\u6e05\u7a7a\u63a7\u5236\u53f0\u4e4b\u524d\u7684\u8f93\u51fa\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u4e2d\u5982\u4f55\u6e05\u7a7a\u63a7\u5236\u53f0\u4e4b\u524d\u7684\u8f93\u51fa\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython\u4e2d\u5982\u4f55\u6e05\u7a7a\u63a7\u5236\u53f0\u4e4b\u524d\u7684\u8f93\u51fa\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173284922967492e4da5400.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u6e05\u7a7a\u4e4b\u524d\u7684\u8f93\u51fa\u5230\u5149\u6807\u4f4d\u7f6e<\/strong><\/p>\n<p>\u8be5\u95ee\u9898\u6d89\u53ca\u5230\u5728 python \u4e2d\u6e05\u9664\u63a7\u5236\u53f0\u8f93\u51fa\u3002\u5f53\u6709\u591a\u4e2a\u8fdb\u5ea6\u6761\u6216\u8f93\u51fa\u540c\u65f6\u51fa\u73b0\u65f6\uff0c\u6709\u65f6\u9700\u8981\u6e05\u7a7a\u4e4b\u524d\u7684\u8f93\u51fa\u4ee5\u5c55\u793a\u65b0\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u884c\u4e3a\uff1a<\/p>\n<ul>\n<li>\n<p><strong>&#8216;r&#8217; \u5b57\u7b26\uff1a<\/strong> r \u7528\u4e8e\u5c06\u5149\u6807\u79fb\u52a8\u5230\u884c\u7684\u5f00\u5934\uff0c\u4ece\u800c\u8986\u76d6\u4e4b\u524d\u7684\u8f93\u51fa\u3002<\/p>\n<pre>for i in range(10):\n  print(f\"{i=}\", end=\"\\r\", flush=true)\n  time.sleep(1)<\/pre>\n<\/li>\n<li>\n<p><strong>ansi \u63a7\u5236\u5b57\u7b26\uff1a<\/strong> ansi \u63a7\u5236\u5b57\u7b26\u5141\u8bb8\u4f60\u63a7\u5236\u7ec8\u7aef\u7684\u5b57\u7b26\u884c\u4e3a\u3002\u4ee5\u4e0b\u4ee3\u7801\u6e05\u9664\u6574\u4e2a\u63a7\u5236\u53f0\uff1a<\/p>\n<pre>import sys\nsys.stdout.write(\"\\033[2j\\033[h\")  # \u6e05\u9664\u6574\u4e2a\u5c4f\u5e55<\/pre>\n<\/li>\n<li>\n<p><strong>\u9488\u5bf9\u884c\u7684\u6e05\u9664\uff1a<\/strong> \u5982\u679c\u4f60\u53ea\u9700\u8981\u6e05\u9664\u7279\u5b9a\u6570\u91cf\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>import sys\ndef clear_lines(num_lines: int):\n  for _i in range(num_lines, -1, -1):\n      # \u79fb\u52a8\u5149\u6807\u5411\u4e0a 'num_lines' \u884c\n      sys.stdout.write(\"\\033[1A\")\n      # \u6e05\u9664\u884c\n      sys.stdout.write(\"\\033[2K\")\n  # \u5c06\u5149\u6807\u79fb\u56de\u7b2c\u4e00\u884c\u5f00\u5934\n  sys.stdout.write(\"\\033[{}G\".format(0))<\/pre>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u6e05\u7a7a\u4e4b\u524d\u7684\u8f93\u51fa\uff0c\u5e76\u53ea\u663e\u793a\u6700\u65b0\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPython\u4e2d\u5982\u4f55\u6e05\u7a7a\u63a7\u5236\u53f0\u4e4b\u524d\u7684\u8f93\u51fa\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u5982\u4f55\u6e05\u7a7a\u63a7\u5236\u53f0\u4e4b\u524d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205333","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205333"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205333\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}