{"id":200,"date":"2023-10-26T16:34:22","date_gmt":"2023-10-26T08:34:22","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=200"},"modified":"2023-10-27T09:36:20","modified_gmt":"2023-10-27T01:36:20","slug":"7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b7. \u8f93\u5165\u548c\u8f93\u51fa"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c55\u73b0\u4e00\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa; \u6570\u636e\u53ef\u4ee5\u4ee5\u4e00\u79cd\u53ef\u8bfb\u7684\u5f62\u5f0f\u8f93\u51fa, \u6216\u8005\u662f\u4fdd\u5b58\u4e8e\u4e00\u4e2a\u6587\u4ef6\u4fbf\u4e8e\u4ee5\u540e\u4f7f\u7528. \u672c\u7ae0\u5c31\u5c06\u8ba8\u8bba\u51e0\u79cd\u53ef\u80fd.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7.1. \u8f93\u51fa\u683c\u5f0f\u7f8e\u5316<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/inputoutput.html#tut-formatting\"><\/a><\/h2>\n\n\n\n<p>\u81f3\u4eca\u4e3a\u6b62, \u6211\u4eec\u77e5\u9053\u4e86\u4e24\u79cd\u8f93\u51fa\u503c\u7684\u65b9\u5f0f:&nbsp;<em>\u8868\u8fbe\u5f0f\u8bed\u53e5<\/em>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>&nbsp;\u51fd\u6570. (\u7b2c\u4e09\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684&nbsp;<code>write()<\/code>&nbsp;\u65b9\u6cd5; \u6807\u51c6\u8f93\u51fa\u6587\u4ef6\u53ef\u4ee5\u7528&nbsp;<code>sys.stdout<\/code>&nbsp;\u5f15\u7528. \u53c2\u8003\u5e93\u624b\u518c\u4e86\u89e3\u66f4\u591a\u7684\u4fe1\u606f.)<\/p>\n\n\n\n<p id=\"index-0\">\u4e00\u822c\u6765\u8bf4\u4f60\u4f1a\u5e0c\u671b\u66f4\u591a\u7684\u63a7\u5236\u5176\u8f93\u51fa\u683c\u5f0f, \u800c\u4e0d\u662f\u7b80\u5355\u7684\u4ee5\u7a7a\u683c\u5206\u5272. \u6709\u4e24\u79cd\u65b9\u5f0f\u683c\u5f0f\u5316\u4f60\u7684\u8f93\u51fa; \u7b2c\u4e00\u79cd\u65b9\u5f0f\u662f\u7531\u4f60\u81ea\u5df1\u63a7\u5236; \u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u548c\u8fde\u63a5\u64cd\u4f5c, \u6765\u5b9e\u73b0\u4f60\u6240\u60f3\u8c61\u7684\u5916\u89c2. \u6807\u51c6\u6a21\u5757&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#module-string\"><code>string<\/code><\/a>&nbsp;\u5305\u542b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u64cd\u4f5c, \u7528\u4ee5\u586b\u5145\u5b57\u7b26\u4e32\u81f3\u67d0\u4e00\u7ed9\u5b9a\u7684\u5bbd\u5ea6; \u5f88\u5feb\u5c31\u4f1a\u8ba8\u8bba\u8fd9\u4e9b. \u7b2c\u4e8c\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>&nbsp;\u65b9\u6cd5.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#module-string\"><code>string<\/code><\/a>&nbsp;\u6a21\u5757\u5305\u542b\u4e86\u4e00\u4e2a\u7c7b\u6a21\u677f, \u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f.<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898, \u5f53\u7136\u4e86: \u5982\u4f55\u628a\u503c\u8f6c\u6210\u5b57\u7b26\u4e32? \u5e78\u8fd0\u7684\u662f, Python \u6709\u591a\u79cd\u65b9\u5f0f\u5c06\u4efb\u4f55\u503c\u8f6c\u4e3a\u5b57\u7b26\u4e32: \u5c06\u5b83\u4f20\u7ed9&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>&nbsp;\u6216&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>&nbsp;\u51fd\u6570.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>&nbsp;\u51fd\u6570\u610f\u5473\u7740\u8fd4\u56de\u4e00\u4e2a\u7528\u6237\u6613\u8bfb\u7684\u8868\u8fbe\u5f62\u5f0f, \u800c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>&nbsp;\u5219\u610f\u5473\u7740\u4ea7\u751f\u4e00\u4e2a\u89e3\u91ca\u5668\u6613\u8bfb\u7684\u8868\u8fbe\u5f62\u5f0f (\u6216\u8005\u5982\u679c\u6ca1\u6709\u8fd9\u6837\u7684\u8bed\u6cd5\u4f1a\u7ed9\u51fa&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#SyntaxError\"><code>SyntaxError<\/code><\/a>&nbsp;). \u5bf9\u4e8e\u90a3\u4e9b\u6ca1\u6709\u7279\u6b8a\u8868\u8fbe\u7684\u5bf9\u8c61,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>&nbsp;\u5c06\u4f1a\u4e0e&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>&nbsp;\u8fd4\u56de\u76f8\u540c\u7684\u503c. \u5f88\u591a\u7684\u503c, \u5982\u6570\u5b57\u6216\u4e00\u4e9b\u5982\u5217\u8868\u548c\u5b57\u5178\u90a3\u6837\u7684\u7ed3\u6784, \u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u7ed3\u679c\u5b8c\u5168\u4e00\u81f4. \u5b57\u7b26\u4e32\u4e0e\u6d6e\u70b9\u578b\u5219\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u8868\u8fbe.<\/p>\n\n\n\n<p>\u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong>s = &#8216;Hello, world.&#8217; <strong>&gt;&gt;&gt; <\/strong>str(s) &#8216;Hello, world.&#8217; <strong>&gt;&gt;&gt; <\/strong>repr(s) &#8220;&#8216;Hello, world.'&#8221; <strong>&gt;&gt;&gt; <\/strong>str(1.0\/7.0) &#8216;0.142857142857&#8217; <strong>&gt;&gt;&gt; <\/strong>repr(1.0\/7.0) &#8216;0.14285714285714285&#8217; <strong>&gt;&gt;&gt; <\/strong>x = 10 * 3.25 <strong>&gt;&gt;&gt; <\/strong>y = 200 * 200 <strong>&gt;&gt;&gt; <\/strong>s = &#8216;The value of x is &#8216; + repr(x) + &#8216;, and y is &#8216; + repr(y) + &#8216;&#8230;&#8217; <strong>&gt;&gt;&gt; <\/strong>print(s) The value of x is 32.5, and y is 40000&#8230; <strong>&gt;&gt;&gt; <\/strong><em># The repr() of a string adds string quotes and backslashes:<\/em><strong>&#8230; <\/strong>hello = &#8216;hello, world<strong>\\n<\/strong>&#8216; <strong>&gt;&gt;&gt; <\/strong>hellos = repr(hello) <strong>&gt;&gt;&gt; <\/strong>print(hellos) &#8216;hello, world\\n&#8217; <strong>&gt;&gt;&gt; <\/strong><em># The argument to repr() may be any Python object:<\/em><strong>&#8230; <\/strong>repr((x, y, (&#8216;spam&#8217;, &#8216;eggs&#8217;))) &#8220;(32.5, 40000, (&#8216;spam&#8217;, &#8216;eggs&#8217;))&#8221;<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u5f0f\u8f93\u51fa\u4e00\u4e2a\u5e73\u65b9\u4e0e\u7acb\u65b9\u7684\u8868:<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> x <strong>in<\/strong> range(1, 11): <strong>&#8230;<\/strong> print(repr(x).rjust(2), repr(x*x).rjust(3), end=&#8217; &#8216;) <strong>&#8230; <\/strong><em># Note use of &#8216;end&#8217; on previous line \u6ce8\u610f\u524d\u4e00\u884c &#8216;end&#8217; \u7684\u4f7f\u7528<\/em><strong>&#8230;<\/strong> print(repr(x*x*x).rjust(4)) <strong>&#8230;<\/strong> 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> x <strong>in<\/strong> range(1, 11): <strong>&#8230;<\/strong> print(&#8216;<em>{0:2d}<\/em><em>{1:3d}<\/em><em>{2:4d}<\/em>&#8216;.format(x, x*x, x*x*x)) <strong>&#8230;<\/strong> 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000<\/p>\n\n\n\n<p>(\u6ce8\u610f\u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d, \u6bcf\u5217\u95f4\u7684\u7a7a\u683c\u662f\u7531&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>&nbsp;\u6dfb\u52a0\u7684: \u5b83\u603b\u4f1a\u5728\u6bcf\u4e2a\u53c2\u6570\u540e\u9762\u52a0\u4e2a\u7a7a\u683c.)<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684&nbsp;<code>rjust()<\/code>&nbsp;\u65b9\u6cd5, \u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u9760\u53f3, \u5e76\u5728\u5de6\u8fb9\u586b\u5145\u7a7a\u683c. \u8fd8\u6709\u7c7b\u4f3c\u7684\u65b9\u6cd5, \u5982&nbsp;<code>ljust()<\/code>&nbsp;\u548c&nbsp;<code>center()<\/code>. \u8fd9\u4e9b\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u5199\u4efb\u4f55\u4e1c\u897f, \u5b83\u4eec\u4ec5\u4ec5\u8fd4\u56de\u65b0\u7684\u5b57\u7b26\u4e32. \u5982\u679c\u8f93\u5165\u5f88\u957f, \u5b83\u4eec\u5e76\u4e0d\u4f1a\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u622a\u65ad, \u4ec5\u4ec5\u8fd4\u56de\u6ca1\u6709\u4efb\u4f55\u53d8\u5316\u7684\u5b57\u7b26\u4e32; \u8fd9\u867d\u7136\u4f1a\u5f71\u54cd\u4f60\u7684\u5e03\u5c40, \u4f46\u662f\u8fd9\u4e00\u822c\u6bd4\u622a\u65ad\u7684\u8981\u597d. (\u5982\u679c\u4f60\u7684\u786e\u9700\u8981\u622a\u65ad, \u90a3\u4e48\u5c31\u589e\u52a0\u4e00\u4e2a\u5207\u7247\u7684\u64cd\u4f5c, \u5982&nbsp;<code>x.ljust(n)[:n]<\/code>.)<\/p>\n\n\n\n<p>\u6709\u53e6\u4e00\u4e2a\u65b9\u6cd5,&nbsp;<code>zfill()<\/code>, \u5b83\u4f1a\u5728\u6570\u5b57\u7684\u5de6\u8fb9\u586b\u5145 0. \u5b83\u77e5\u9053\u6b63\u8d1f\u53f7:<strong>&gt;&gt;&gt; <\/strong>&#8217;12&#8217;.zfill(5) &#8216;00012&#8217; <strong>&gt;&gt;&gt; <\/strong>&#8216;-3.14&#8217;.zfill(7) &#8216;-003.14&#8217; <strong>&gt;&gt;&gt; <\/strong>&#8216;3.14159265359&#8217;.zfill(5) &#8216;3.14159265359&#8217;<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>&nbsp;\u7684\u57fa\u672c\u4f7f\u7528\u5982\u4e0b:<strong>&gt;&gt;&gt; <\/strong>print(&#8216;We are the <em>{}<\/em> who say &#8220;<em>{}<\/em>!&#8221;&#8216;.format(&#8216;knights&#8217;, &#8216;Ni&#8217;)) We are the knights who say &#8220;Ni!&#8221;<\/p>\n\n\n\n<p>\u62ec\u53f7\u53ca\u5176\u91cc\u9762\u7684\u5b57\u7b26 (\u79f0\u4f5c format field) \u5c06\u4f1a\u88ab&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>&nbsp;\u4e2d\u7684\u53c2\u6570\u66ff\u6362. \u5728\u62ec\u53f7\u4e2d\u7684\u6570\u5b57\u7528\u4e8e\u6307\u5411\u4f20\u5165\u5bf9\u8c61\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>&nbsp;\u4e2d\u7684\u4f4d\u7f6e.<strong>&gt;&gt;&gt; <\/strong>print(&#8216;<em>{0}<\/em> and <em>{1}<\/em>&#8216;.format(&#8216;spam&#8217;, &#8216;eggs&#8217;)) spam and eggs <strong>&gt;&gt;&gt; <\/strong>print(&#8216;<em>{1}<\/em> and <em>{0}<\/em>&#8216;.format(&#8216;spam&#8217;, &#8216;eggs&#8217;)) eggs and spam<\/p>\n\n\n\n<p>\u5982\u679c\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>&nbsp;\u4e2d\u4f7f\u7528\u4e86\u5173\u952e\u5b57\u53c2\u6570, \u90a3\u4e48\u5b83\u4eec\u7684\u503c\u4f1a\u6307\u5411\u4f7f\u7528\u8be5\u540d\u5b57\u7684\u53c2\u6570.<strong>&gt;&gt;&gt; <\/strong>print(&#8216;This <em>{food}<\/em> is <em>{adjective}<\/em>.&#8217;.format( <strong>&#8230;<\/strong> food=&#8217;spam&#8217;, adjective=&#8217;absolutely horrible&#8217;)) This spam is absolutely horrible.<\/p>\n\n\n\n<p>\u4f4d\u7f6e\u53ca\u5173\u952e\u5b57\u53c2\u6570\u53ef\u4ee5\u4efb\u610f\u7684\u7ed3\u5408:<strong>&gt;&gt;&gt; <\/strong>print(&#8216;The story of <em>{0}<\/em>, <em>{1}<\/em>, and <em>{other}<\/em>.&#8217;.format(&#8216;Bill&#8217;, &#8216;Manfred&#8217;, other=&#8217;Georg&#8217;)) The story of Bill, Manfred, and Georg.<\/p>\n\n\n\n<p><code>'!a'<\/code>&nbsp;(\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#ascii\"><code>ascii()<\/code><\/a>),&nbsp;<code>'!s'<\/code>&nbsp;(\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>) \u548c&nbsp;<code>'!r'<\/code>&nbsp;(\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>) \u53ef\u4ee5\u7528\u4e8e\u5728\u683c\u5f0f\u5316\u67d0\u4e2a\u503c\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u8f6c\u5316:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>math<\/strong><strong>&gt;&gt;&gt; <\/strong>print(&#8216;The value of PI is approximately <em>{}<\/em>.&#8217;.format(math.pi)) The value of PI is approximately 3.14159265359. <strong>&gt;&gt;&gt; <\/strong>print(&#8216;The value of PI is approximately <em>{!r}<\/em>.&#8217;.format(math.pi)) The value of PI is approximately 3.141592653589793.<\/p>\n\n\n\n<p>\u53ef\u9009\u9879&nbsp;<code>':'<\/code>&nbsp;\u548c\u683c\u5f0f\u6807\u8bc6\u7b26\u53ef\u4ee5\u8ddf\u7740 field name. \u8fd9\u5c31\u5141\u8bb8\u5bf9\u503c\u8fdb\u884c\u66f4\u597d\u7684\u683c\u5f0f\u5316. \u4e0b\u9762\u7684\u4f8b\u5b50\u5c06 Pi \u4fdd\u7559\u5230\u5c0f\u6570\u70b9\u540e\u4e09\u4f4d.<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>math<\/strong><strong>&gt;&gt;&gt; <\/strong>print(&#8216;The value of PI is approximately <em>{0:.3f}<\/em>.&#8217;.format(math.pi)) The value of PI is approximately 3.142.<\/p>\n\n\n\n<p>\u5728&nbsp;<code>':'<\/code>&nbsp;\u540e\u4f20\u5165\u4e00\u4e2a\u6574\u6570, \u53ef\u4ee5\u4fdd\u8bc1\u8be5\u57df\u81f3\u5c11\u6709\u8fd9\u4e48\u591a\u7684\u5bbd\u5ea6. \u7528\u4e8e\u7f8e\u5316\u8868\u683c\u65f6\u5f88\u6709\u7528.<strong>&gt;&gt;&gt; <\/strong>table = {&#8216;Sjoerd&#8217;: 4127, &#8216;Jack&#8217;: 4098, &#8216;Dcab&#8217;: 7678} <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> name, phone <strong>in<\/strong> table.items(): <strong>&#8230;<\/strong> print(&#8216;<em>{0:10}<\/em> ==&gt; <em>{1:10d}<\/em>&#8216;.format(name, phone)) <strong>&#8230;<\/strong> Jack ==&gt; 4098 Dcab ==&gt; 7678 Sjoerd ==&gt; 4127<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7684\u786e\u5f88\u957f\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32, \u800c\u4f60\u4e0d\u60f3\u5c06\u5b83\u4eec\u5206\u5f00, \u90a3\u4e48\u5728\u683c\u5f0f\u5316\u65f6\u901a\u8fc7\u53d8\u91cf\u540d\u800c\u975e\u4f4d\u7f6e\u4f1a\u662f\u5f88\u597d\u7684\u4e8b\u60c5. \u6700\u7b80\u5355\u7684\u5c31\u662f\u4f20\u5165\u4e00\u4e2a\u5b57\u5178, \u7136\u540e\u4f7f\u7528\u65b9\u62ec\u53f7&nbsp;<code>'[]'<\/code>&nbsp;\u6765\u8bbf\u95ee\u952e\u503c :<strong>&gt;&gt;&gt; <\/strong>table = {&#8216;Sjoerd&#8217;: 4127, &#8216;Jack&#8217;: 4098, &#8216;Dcab&#8217;: 8637678} <strong>&gt;&gt;&gt; <\/strong>print(&#8216;Jack: <em>{0[Jack]:d}<\/em>; Sjoerd: <em>{0[Sjoerd]:d}<\/em>; &#8216; &#8216;Dcab: {0[Dcab]:d}&#8217;.format(table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678<\/p>\n\n\n\n<p>\u8fd9\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728 table \u53d8\u91cf\u524d\u4f7f\u7528 \u2018**\u2019 \u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd.<strong>&gt;&gt;&gt; <\/strong>table = {&#8216;Sjoerd&#8217;: 4127, &#8216;Jack&#8217;: 4098, &#8216;Dcab&#8217;: 8637678} <strong>&gt;&gt;&gt; <\/strong>print(&#8216;Jack: <em>{Jack:d}<\/em>; Sjoerd: <em>{Sjoerd:d}<\/em>; Dcab: <em>{Dcab:d}<\/em>&#8216;.format(**table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678<\/p>\n\n\n\n<p>\u5728\u7ed3\u5408\u65b0\u7684\u5185\u7f6e\u51fd\u6570&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#vars\"><code>vars()<\/code><\/a>&nbsp;(\u8fd9\u4f1a\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u7684\u5c40\u90e8\u53d8\u91cf) \u548c\u8fd9\u4e2a\u65f6\u4f1a\u7279\u522b\u6709\u7528.<\/p>\n\n\n\n<p>\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>&nbsp;\u7684\u77e5\u8bc6, \u53c2\u8003&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#formatstrings\">Format String Syntax<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.1.1. \u65e7\u5f0f\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/inputoutput.html#id9\"><\/a><\/h3>\n\n\n\n<p><code>%<\/code>&nbsp;\u64cd\u4f5c\u7b26\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u683c\u5f0f\u5316. \u5b83\u5c06\u5de6\u8fb9\u7684\u53c2\u6570\u4f5c\u4e3a\u7c7b\u4f3c&nbsp;<code>sprintf()<\/code>&nbsp;\u5f0f\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32, \u800c\u5c06\u53f3\u8fb9\u7684\u4ee3\u5165, \u7136\u540e\u8fd4\u56de\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32. \u4f8b\u5982:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>math<\/strong><strong>&gt;&gt;&gt; <\/strong>print(&#8216;The value of PI is approximately <em>%5.3f<\/em>.&#8217; % math.pi) The value of PI is approximately 3.142.<\/p>\n\n\n\n<p>\u56e0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>&nbsp;\u5f88\u65b0, \u5927\u591a\u6570\u7684 Python \u4ee3\u7801\u4ecd\u7136\u4f7f\u7528&nbsp;<code>%<\/code>&nbsp;\u64cd\u4f5c\u7b26. \u4f46\u662f\u56e0\u4e3a\u8fd9\u79cd\u65e7\u5f0f\u7684\u683c\u5f0f\u5316\u6700\u7ec8\u4f1a\u4ece\u8be5\u8bed\u8a00\u4e2d\u79fb\u9664, \u5e94\u8be5\u66f4\u591a\u7684\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>.<\/p>\n\n\n\n<p>\u66f4\u591a\u7684\u4fe1\u606f\u53ef\u4ee5\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#old-string-formatting\">Old String Formatting Operations<\/a>&nbsp;\u4e2d\u627e\u5230.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7.2. \u8bfb\u548c\u5199\u6587\u4ef6<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/inputoutput.html#tut-files\"><\/a><\/h2>\n\n\n\n<p id=\"index-1\"><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#open\"><code>open()<\/code><\/a>&nbsp;\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/glossary.html#term-file-object\">file object<\/a>, \u5e76\u4e14\u4e00\u822c\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\u8fdb\u884c\u4f7f\u7528:&nbsp;<code>open(filename,&nbsp;mode)<\/code>.<strong>&gt;&gt;&gt; <\/strong>f = open(&#8216;\/tmp\/workfile&#8217;, &#8216;w&#8217;)<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5305\u542b\u6587\u4ef6\u540d\u7684\u5b57\u7b26\u4e32. \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32, \u5305\u542b\u63cf\u8ff0\u6587\u4ef6\u5982\u4f55\u4f7f\u7528\u7684\u5b57\u7b26.&nbsp;<em>mode<\/em>&nbsp;\u53ef\u4ee5\u662f&nbsp;<code>'r'<\/code>&nbsp;\u5982\u679c\u6587\u4ef6\u53ea\u8bfb,&nbsp;<code>'w'<\/code>&nbsp;\u53ea\u7528\u4e8e\u5199 (\u5982\u679c\u5b58\u5728\u540c\u540d\u6587\u4ef6\u5219\u5c06\u88ab\u5220\u9664), \u548c&nbsp;<code>'a'<\/code>&nbsp;\u7528\u4e8e\u8ffd\u52a0\u6587\u4ef6\u5185\u5bb9; \u6240\u5199\u7684\u4efb\u4f55\u6570\u636e\u90fd\u4f1a\u88ab\u81ea\u52a8\u589e\u52a0\u5230\u672b\u5c3e.&nbsp;<code>'r+'<\/code>&nbsp;\u540c\u65f6\u7528\u4e8e\u8bfb\u5199.&nbsp;<em>mode<\/em>&nbsp;\u53c2\u6570\u662f\u53ef\u9009\u7684;&nbsp;<code>'r'<\/code>&nbsp;\u5c06\u662f\u9ed8\u8ba4\u503c.<\/p>\n\n\n\n<p>\u4e00\u822c\u800c\u8a00, \u6587\u4ef6\u4ee5&nbsp;<em>text mode<\/em>&nbsp;\u6253\u5f00, \u8fd9\u5c31\u610f\u5473\u7740, \u4ece\u6587\u4ef6\u4e2d\u8bfb\u5199\u7684\u5b57\u7b26\u4e32, \u662f\u4ee5\u4e00\u79cd\u7279\u5b9a\u7684\u7f16\u7801\u8fdb\u884c\u7f16\u7801 (\u9ed8\u8ba4\u7684\u662f UTF-8). \u8ffd\u52a0\u5230&nbsp;<em>mode<\/em>&nbsp;\u540e\u7684&nbsp;<code>'b'<\/code>&nbsp;, \u5c06\u610f\u5473\u7740\u4ee5&nbsp;<em>binary mode<\/em>&nbsp;\u6253\u5f00\u6587\u4ef6: \u73b0\u5728\u7684\u6570\u636e\u662f\u4ee5\u5b57\u8282\u5bf9\u8c61\u7684\u5f62\u5f0f\u8fdb\u884c\u8bfb\u5199. \u8fd9\u4e2a\u6a21\u5f0f\u5e94\u8be5\u7528\u4e8e\u90a3\u4e9b\u4e0d\u5305\u542b\u6587\u672c\u7684\u6587\u4ef6.<\/p>\n\n\n\n<p>\u5728\u6587\u672c\u6a21\u5f0f\u4e0b (text mode), \u9ed8\u8ba4\u662f\u5c06\u7279\u5b9a\u5e73\u53f0\u7684\u884c\u672b\u6807\u8bc6\u7b26 ( Unix \u4e0b\u4e3a&nbsp;<code>\\n<\/code>, Windows \u4e0b\u4e3a&nbsp;<code>\\r\\n<\/code>&nbsp;) \u5728\u8bfb\u65f6\u8f6c\u4e3a&nbsp;<code>\\n<\/code>&nbsp;\u800c\u5199\u65f6\u5c06&nbsp;<code>\\n<\/code>&nbsp;\u8f6c\u4e3a\u7279\u5b9a\u5e73\u53f0\u7684\u6807\u8bc6\u7b26. \u8fd9\u79cd\u9690\u85cf\u7684\u884c\u4e3a\u5bf9\u4e8e\u6587\u672c\u6587\u4ef6\u662f\u6ca1\u6709\u95ee\u9898\u7684, \u4f46\u662f\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e\u50cf&nbsp;<code>JPEG<\/code>&nbsp;\u6216&nbsp;<code>EXE<\/code>&nbsp;\u662f\u4f1a\u51fa\u95ee\u9898\u7684. \u5728\u4f7f\u7528\u8fd9\u4e9b\u6587\u4ef6\u65f6\u8bf7\u5c0f\u5fc3\u4f7f\u7528\u4e8c\u8fdb\u5236\u6a21\u5f0f.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.2.1. \u6587\u4ef6\u5bf9\u8c61\u7684\u65b9\u6cd5<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/inputoutput.html#tut-filemethods\"><\/a><\/h3>\n\n\n\n<p>\u672c\u8282\u4e2d\u5269\u4e0b\u7684\u4f8b\u5b50\u5047\u8bbe\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u79f0\u4e3a&nbsp;<code>f<\/code>&nbsp;\u7684\u6587\u4ef6\u5bf9\u8c61.<\/p>\n\n\n\n<p>\u4e3a\u4e86\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9, \u8c03\u7528&nbsp;<code>f.read(size)<\/code>, \u8fd9\u5c06\u8bfb\u53d6\u4e00\u5b9a\u6570\u76ee\u7684\u6570\u636e, \u7136\u540e\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6216\u5b57\u8282\u5bf9\u8c61\u8fd4\u56de.&nbsp;<em>size<\/em>&nbsp;\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6570\u5b57\u7c7b\u578b\u7684\u53c2\u6570. \u5f53&nbsp;<em>size<\/em>&nbsp;\u88ab\u5ffd\u7565\u4e86\u6216\u8005\u4e3a\u8d1f, \u90a3\u4e48\u8be5\u6587\u4ef6\u7684\u6240\u6709\u5185\u5bb9\u90fd\u5c06\u88ab\u8bfb\u53d6\u5e76\u4e14\u8fd4\u56de; \u5982\u679c\u6587\u4ef6\u6bd4\u4f60\u7684\u5185\u5b58\u5927\u4e24\u500d, \u90a3\u4e48\u5c31\u4f1a\u6210\u4e3a\u4f60\u7684\u95ee\u9898\u4e86. \u5426\u5219, \u6700\u591a&nbsp;<em>size<\/em>&nbsp;\u5b57\u8282\u5c06\u88ab\u8bfb\u53d6\u5e76\u8fd4\u56de. \u5982\u679c\u5230\u8fbe\u4e86\u6587\u4ef6\u7684\u672b\u5c3e,&nbsp;<code>f.read()<\/code>&nbsp;\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32 (<code>''<\/code>).<strong>&gt;&gt;&gt; <\/strong>f.read() &#8216;This is the entire file.\\n&#8217; <strong>&gt;&gt;&gt; <\/strong>f.read() &#8221;<\/p>\n\n\n\n<p><code>f.readline()<\/code>&nbsp;\u4f1a\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5355\u72ec\u7684\u4e00\u884c; \u5728\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u90fd\u4f1a\u7559\u4e0b\u6362\u884c\u7b26 (<code>\\n<\/code>), \u9664\u975e\u662f\u8be5\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u5e76\u4e14\u6ca1\u6709\u4ee5\u6362\u884c\u7b26\u7ed3\u675f, \u8fd9\u4e2a\u5b57\u7b26\u624d\u4f1a\u88ab\u5ffd\u7565. \u8fd9\u5c31\u4f7f\u7ed3\u679c\u5f88\u660e\u786e;&nbsp;<code>f.readline()<\/code>&nbsp;\u5982\u679c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32, \u90a3\u4e48\u6587\u4ef6\u5df2\u5230\u5e95\u4e86, \u800c\u5982\u679c\u662f\u4ee5&nbsp;<code>'\\n'<\/code>&nbsp;\u8868\u793a, \u90a3\u4e48\u5c31\u662f\u53ea\u5305\u884c\u4e00\u4e2a\u65b0\u884c.<strong>&gt;&gt;&gt; <\/strong>f.readline() &#8216;This is the first line of the file.\\n&#8217; <strong>&gt;&gt;&gt; <\/strong>f.readline() &#8216;Second line of the file\\n&#8217; <strong>&gt;&gt;&gt; <\/strong>f.readline() &#8221;<\/p>\n\n\n\n<p><code>f.readlines()<\/code>&nbsp;\u5c06\u8fd4\u56de\u8be5\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u6240\u6709\u884c. \u5982\u679c\u7ed9\u5b9a\u4e00\u4e2a\u53ef\u9009\u53c2\u6570&nbsp;<em>sizehint<\/em>, \u5b83\u5c31\u8bfb\u53d6\u8fd9\u4e48\u591a\u5b57\u8282, \u5e76\u4e14\u5c06\u8fd9\u4e9b\u5b57\u8282\u6309\u884c\u5206\u5272. \u8fd9\u7ecf\u5e38\u7528\u4e8e\u5141\u8bb8\u6309\u884c\u8bfb\u53d6\u4e00\u4e2a\u5927\u6587\u4ef6, \u4f46\u662f\u4e0d\u9700\u8981\u8f7d\u5165\u5168\u90e8\u7684\u6587\u4ef6\u65f6\u975e\u5e38\u6709\u7528. \u53ea\u4f1a\u8fd4\u56de\u5b8c\u6574\u7684\u884c.<strong>&gt;&gt;&gt; <\/strong>f.readlines() [&#8216;This is the first line of the file.\\n&#8217;, &#8216;Second line of the file\\n&#8217;]<\/p>\n\n\n\n<p>\u53e6\u4e00\u79cd\u65b9\u5f0f\u662f\u8fed\u4ee3\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u7136\u540e\u8bfb\u53d6\u6bcf\u884c. \u8fd9\u662f\u5185\u5b58\u6709\u6548, \u5feb\u901f, \u5e76\u7528\u6700\u5c11\u7684\u4ee3\u7801:<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> line <strong>in<\/strong> f: <strong>&#8230;<\/strong> print(line, end=&#8221;) <strong>&#8230;<\/strong> This is the first line of the file. Second line of the file<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5f88\u7b80\u5355, \u4f46\u662f\u5e76\u6ca1\u6709\u63d0\u4f9b\u4e00\u4e2a\u5f88\u597d\u7684\u63a7\u5236. \u56e0\u4e3a\u4e24\u8005\u7684\u5904\u7406\u673a\u5236\u4e0d\u540c, \u6700\u597d\u4e0d\u8981\u6df7\u7528.<\/p>\n\n\n\n<p><code>f.write(string)<\/code>&nbsp;\u5c06&nbsp;<em>string<\/em>&nbsp;\u5199\u5165\u5230\u6587\u4ef6\u4e2d, \u7136\u540e\u8fd4\u56de\u5199\u5165\u7684\u5b57\u7b26\u6570.<strong>&gt;&gt;&gt; <\/strong>f.write(&#8216;This is a test<strong>\\n<\/strong>&#8216;) 15<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u5199\u5165\u4e00\u4e9b\u4e0d\u662f\u5b57\u7b26\u4e32\u7684\u4e1c\u897f, \u90a3\u4e48\u5c06\u9700\u8981\u5148\u8fdb\u884c\u8f6c\u6362:<strong>&gt;&gt;&gt; <\/strong>value = (&#8216;the answer&#8217;, 42) <strong>&gt;&gt;&gt; <\/strong>s = str(value) <strong>&gt;&gt;&gt; <\/strong>f.write(s) 18<\/p>\n\n\n\n<p><code>f.tell()<\/code>&nbsp;\u8fd4\u56de\u6587\u4ef6\u5bf9\u8c61\u5f53\u524d\u6240\u5904\u7684\u4f4d\u7f6e, \u5b83\u662f\u4ece\u6587\u4ef6\u5f00\u5934\u5f00\u59cb\u7b97\u8d77\u7684\u5b57\u8282\u6570. \u8981\u6539\u53d8\u6587\u4ef6\u5f53\u524d\u7684\u4f4d\u7f6e, \u4f7f\u7528&nbsp;<code>f.seek(offset,&nbsp;from_what)<\/code>. \u8fd9\u4e2a\u4f4d\u7f6e\u662f\u901a\u8fc7\u5c06\u5f53\u524d\u4f4d\u7f6e\u52a0\u4e0a&nbsp;<em>offset<\/em>&nbsp;\u6240\u5f97.&nbsp;<em>from_what<\/em>&nbsp;\u7684\u503c, \u5982\u679c\u662f 0 \u8868\u793a\u5f00\u5934, \u5982\u679c\u662f 1 \u8868\u793a\u5f53\u524d\u4f4d\u7f6e, 2 \u8868\u793a\u6587\u4ef6\u7684\u7ed3\u5c3e.&nbsp;<em>from_what<\/em>&nbsp;\u7684\u9ed8\u8ba4\u4e3a 0, \u5373\u4ece\u5f00\u5934\u5f00\u59cb.<strong>&gt;&gt;&gt; <\/strong>f = open(&#8216;\/tmp\/workfile&#8217;, &#8216;rb+&#8217;) <strong>&gt;&gt;&gt; <\/strong>f.write(b&#8217;0123456789abcdef&#8217;) 16 <strong>&gt;&gt;&gt; <\/strong>f.seek(5) <em># Go to the 6th byte in the file<\/em> 5 <strong>&gt;&gt;&gt; <\/strong>f.read(1) b&#8217;5&#8242; <strong>&gt;&gt;&gt; <\/strong>f.seek(-3, 2) <em># Go to the 3rd byte before the end<\/em> 13 <strong>&gt;&gt;&gt; <\/strong>f.read(1) b&#8217;d&#8217;<\/p>\n\n\n\n<p>\u5728\u6587\u672c\u6587\u4ef6\u4e2d (\u90a3\u4e9b\u6253\u5f00\u6587\u4ef6\u7684\u6a21\u5f0f\u4e0b\u6ca1\u6709&nbsp;<code>b<\/code>&nbsp;\u7684), \u53ea\u4f1a\u76f8\u5bf9\u4e8e\u6587\u4ef6\u8d77\u59cb\u4f4d\u7f6e\u8fdb\u884c\u5b9a\u4f4d, (\u5982\u679c\u8981\u5b9a\u6587\u4ef6\u7684\u6700\u540e\u9762, \u8981\u7528&nbsp;<code>seek(0,&nbsp;2)<\/code>&nbsp;).<\/p>\n\n\n\n<p>\u5f53\u4f60\u5904\u7406\u5b8c\u4e00\u4e2a\u6587\u4ef6\u540e, \u8c03\u7528&nbsp;<code>f.close()<\/code>&nbsp;\u4f1a\u5173\u95ed\u5b83, \u5e76\u91ca\u653e\u7cfb\u7edf\u7684\u8d44\u6e90. \u5728\u8c03\u7528\u5b8c&nbsp;<code>f.close()<\/code>&nbsp;\u4e4b\u540e, \u5c1d\u8bd5\u4f7f\u7528\u90a3\u4e2a\u6587\u4ef6\u5bf9\u8c61\u662f\u4f1a\u5931\u8d25\u7684.<strong>&gt;&gt;&gt; <\/strong>f.close() <strong>&gt;&gt;&gt; <\/strong>f.read() Traceback (most recent call last): File &#8220;&lt;stdin&gt;&#8221;, line 1, in ? ValueError: I\/O operation on closed file<\/p>\n\n\n\n<p>\u5f53\u5904\u7406\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u65f6, \u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#with\"><code>with<\/code><\/a>&nbsp;\u5173\u952e\u5b57\u662f\u975e\u5e38\u597d\u7684\u65b9\u5f0f. \u5728\u7ed3\u675f\u540e, \u5b83\u4f1a\u5e2e\u4f60\u6b63\u786e\u7684\u5173\u95ed\u6587\u4ef6, \u5373\u4f7f\u53d1\u751f\u4e86\u5f02\u5e38. \u800c\u4e14\u5199\u8d77\u6765\u4e5f\u6bd4&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;&#8211;&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>&nbsp;\u8bed\u53e5\u5757\u8981\u7b80\u77ed:<strong>&gt;&gt;&gt; <\/strong><strong>with<\/strong> open(&#8216;\/tmp\/workfile&#8217;, &#8216;r&#8217;) <strong>as<\/strong> f: <strong>&#8230;<\/strong> read_data = f.read() <strong>&gt;&gt;&gt; <\/strong>f.closed True<\/p>\n\n\n\n<p>\u6587\u4ef6\u5bf9\u8c61\u6709\u4e9b\u989d\u5916\u7684\u65b9\u6cd5, \u5982&nbsp;<code>isatty()<\/code>&nbsp;\u548c&nbsp;<code>trucate()<\/code>, \u4f46\u5b83\u4eec\u90fd\u8f83\u5c11\u7684\u4f7f\u7528; \u66f4\u591a\u7684\u4fe1\u606f\u9700\u8981\u53c2\u8003\u6807\u51c6\u5e93\u624b\u518c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.2.2.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>&nbsp;\u6a21\u5757<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/inputoutput.html#pickle\"><\/a><\/h3>\n\n\n\n<p id=\"index-2\">\u5728\u6587\u4ef6\u4e2d, \u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u8bfb\u53d6\u5199\u5165. \u6570\u5b57\u53ef\u80fd\u7a0d\u5fae\u9ebb\u70e6\u4e00\u4e9b, \u56e0\u4e3a&nbsp;<code>read()<\/code>&nbsp;\u65b9\u6cd5\u53ea\u8fd4\u56de\u5b57\u7b26\u4e32, \u6211\u4eec\u8fd8\u9700\u8981\u5c06\u5176\u4f20\u7ed9&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#int\"><code>int()<\/code><\/a>&nbsp;\u8fd9\u6837\u7684\u51fd\u6570, \u4f7f\u5176\u5c06\u5982&nbsp;<code>'123'<\/code>&nbsp;\u7684\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6570\u5b57 123. \u4f46\u662f, \u5982\u679c\u8981\u4fdd\u5b58\u66f4\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b, \u5982\u5217\u8868, \u5b57\u5178, \u6216\u8005\u7c7b\u7684\u5b9e\u4f8b, \u90a3\u4e48\u5c31\u4f1a\u66f4\u590d\u6742\u4e86.<\/p>\n\n\n\n<p>\u4e3a\u4e86\u8ba9\u7528\u6237\u5728\u65f6\u5e38\u7684\u7f16\u7a0b\u548c\u6d4b\u8bd5\u65f6\u4fdd\u5b58\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b, Python \u63d0\u4f9b\u4e86\u6807\u51c6\u6a21\u5757, \u79f0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>. \u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u5c06\u51e0\u4e4e\u4efb\u4f55\u7684 Python \u5bf9\u8c61 (\u751a\u81f3\u662f Python \u7684\u4ee3\u7801), \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8868\u793a; \u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a&nbsp;<em>pickling<\/em>. \u800c\u8981\u4ece\u91cc\u9762\u91cd\u65b0\u6784\u9020\u56de\u539f\u6765\u7684\u5bf9\u8c61, \u5219\u79f0\u4e3a&nbsp;<em>unpickling<\/em>. \u5728 pickling \u548c unpickling \u4e4b\u95f4, \u8868\u793a\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a, \u53ef\u4ee5\u5b58\u4e8e\u4e00\u4e2a\u6587\u4ef6, \u4e5f\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u5728\u8fdc\u7a0b\u673a\u5668\u95f4\u4f20\u8f93.<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5bf9\u8c61&nbsp;<code>x<\/code>, \u548c\u4e00\u4e2a\u5df2\u7ecf\u6253\u5f00\u5e76\u7528\u4e8e\u5199\u7684\u6587\u4ef6\u5bf9\u8c61&nbsp;<code>f<\/code>, pickle \u8fd9\u4e2a\u5bf9\u8c61\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528:pickle.dump(x, f)<\/p>\n\n\n\n<p>\u6709\u4e86 pickle \u8fd9\u4e2a\u5bf9\u8c61, \u5c31\u80fd\u5bf9&nbsp;<code>f<\/code>&nbsp;\u4ee5\u8bfb\u53d6\u7684\u5f62\u5f0f\u6253\u5f00:x = pickle.load(f)<\/p>\n\n\n\n<p>(\u8fd8\u6709\u5176\u4ed6\u4e0d\u540c\u7684\u5f62\u5f0f, \u6bd4\u5982 pickling \u5f88\u591a\u5bf9\u8c61, \u6216\u8005\u4e0d\u60f3\u4fdd\u5b58\u81f3\u6587\u4ef6; \u66f4\u591a\u7684\u4fe1\u606f\u53c2\u8003&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>&nbsp;\u6a21\u5757.)<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>&nbsp;\u662f Python \u4e2d\u4fdd\u5b58\u53ca\u91cd\u7528\u5bf9\u8c61\u7684\u6807\u51c6\u65b9\u5f0f; \u6807\u51c6\u7684\u5c5e\u4e8e\u79f0\u4e3a&nbsp;<em>persistent<\/em>&nbsp;\u5bf9\u8c61 (\u5373\u6301\u4e45\u5316\u5bf9\u8c61). \u56e0\u4e3a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>&nbsp;\u88ab\u5e7f\u6cdb\u4f7f\u7528, \u5f88\u591a\u5199 Python \u6269\u5c55\u7684\u4f5c\u8005\u90fd\u4f1a\u786e\u4fdd, \u5982\u77e9\u9635\u8fd9\u6837\u7684\u6570\u636e\u7c7b\u578b\u80fd\u88ab\u5408\u7406\u7684 pickle \u548c unpickle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c55\u73b0\u4e00\u4e2a\u7a0b\u5e8f\u7684\u8f93\u51fa; \u6570\u636e\u53ef\u4ee5\u4ee5\u4e00\u79cd\u53ef\u8bfb\u7684\u5f62\u5f0f\u8f93\u51fa, \u6216\u8005\u662f\u4fdd\u5b58\u4e8e\u4e00\u4e2a\u6587\u4ef6\u4fbf\u4e8e\u4ee5\u540e\u4f7f\u7528. \u672c\u7ae0\u5c31 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":3,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":252,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions\/252"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}