{"id":273,"date":"2023-10-27T09:56:35","date_gmt":"2023-10-27T01:56:35","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=273"},"modified":"2023-10-31T15:39:35","modified_gmt":"2023-10-31T07:39:35","slug":"%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b7-%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\/27\/%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b7-%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<p><\/p>\n\n\n\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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>s = \u2018Hello, world.\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>str(s) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2018Hello, world.\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>repr(s)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u201c\u2018Hello, world.&#8217;\u201d<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>str(1.0\/7.0)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u20180.142857142857\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>repr(1.0\/7.0) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u20180.14285714285714285\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>x = 10 * 3.25<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>y = 200 * 200\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>s = \u2018The value of x is \u2018 + repr(x) + \u2018, and y is \u2018 + repr(y) + \u2018\u2026\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>print(s) The value of x is 32.5, and y is 40000\u2026<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong><em># The repr() of a string adds string quotes and backslashes:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0<\/strong>hello = \u2018hello, world<strong>\\n<\/strong>\u2018\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>hellos = repr(hello)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(hellos) \u2018hello, world\\n\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong><em># The argument to repr() may be any Python object:<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0<\/strong>repr((x, y, (\u2018spam\u2019, \u2018eggs\u2019))) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u201c(32.5, 40000, (\u2018spam\u2019, \u2018eggs\u2019))\u201d<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u5f0f\u8f93\u51fa\u4e00\u4e2a\u5e73\u65b9\u4e0e\u7acb\u65b9\u7684\u8868:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"510\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-6.png\" alt=\"\" class=\"wp-image-372\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-6.png 900w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-6-300x170.png 300w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-6-768x435.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\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,\u00a0<code>zfill()<\/code>, \u5b83\u4f1a\u5728\u6570\u5b57\u7684\u5de6\u8fb9\u586b\u5145 0. \u5b83\u77e5\u9053\u6b63\u8d1f\u53f7:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>\u201912\u2019.zfill(5) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u201800012\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>\u2018-3.14\u2019.zfill(7)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2018-003.14\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>\u20183.14159265359\u2019.zfill(5)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u20183.14159265359\u2019<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>str.format()<\/code><\/a>\u00a0\u7684\u57fa\u672c\u4f7f\u7528\u5982\u4e0b:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018We are the\u00a0<em>{}<\/em>\u00a0who say \u201c<em>{}<\/em>!\u201d\u2018.format(\u2018knights\u2019, \u2018Ni\u2019))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> We are the knights who say \u201cNi!\u201d<\/p>\n\n\n\n<p>\u62ec\u53f7\u53ca\u5176\u91cc\u9762\u7684\u5b57\u7b26 (\u79f0\u4f5c format field) \u5c06\u4f1a\u88ab\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>\u00a0\u4e2d\u7684\u53c2\u6570\u66ff\u6362. \u5728\u62ec\u53f7\u4e2d\u7684\u6570\u5b57\u7528\u4e8e\u6307\u5411\u4f20\u5165\u5bf9\u8c61\u5728\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>\u00a0\u4e2d\u7684\u4f4d\u7f6e.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018<em>{0}<\/em>\u00a0and\u00a0<em>{1}<\/em>\u2018.format(\u2018spam\u2019, \u2018eggs\u2019))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> spam and eggs<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>print(\u2018<em>{1}<\/em>\u00a0and\u00a0<em>{0}<\/em>\u2018.format(\u2018spam\u2019, \u2018eggs\u2019))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> eggs and spam<\/p>\n\n\n\n<p>\u5982\u679c\u5728\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/stdtypes.html#str.format\"><code>format()<\/code><\/a>\u00a0\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.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018This\u00a0<em>{food}<\/em>\u00a0is\u00a0<em>{adjective}<\/em>.\u2019.format(<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0food=\u2019spam\u2019, adjective=\u2019absolutely horrible\u2019)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018The story of\u00a0<em>{0}<\/em>,\u00a0<em>{1}<\/em>, and\u00a0<em>{other}<\/em>.\u2019.format(\u2018Bill\u2019, \u2018Manfred\u2019,<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> other=\u2019Georg\u2019)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">The story of Bill, Manfred, and Georg.<\/p>\n\n\n\n<p><code>'!a'<\/code>\u00a0(\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#ascii\"><code>ascii()<\/code><\/a>),\u00a0<code>'!s'<\/code>\u00a0(\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#str\"><code>str()<\/code><\/a>) \u548c\u00a0<code>'!r'<\/code>\u00a0(\u4f7f\u7528\u00a0<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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importmath<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018The value of PI is approximately\u00a0<em>{}<\/em>.\u2019.format(math.pi)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">The value of PI is approximately 3.14159265359.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>print(\u2018The value of PI is approximately\u00a0<em>{!r}<\/em>.\u2019.format(math.pi)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">The value of PI is approximately 3.141592653589793.<\/p>\n\n\n\n<p>\u53ef\u9009\u9879\u00a0<code>':'<\/code>\u00a0\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.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importmath<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018The value of PI is approximately\u00a0<em>{0:.3f}<\/em>.\u2019.format(math.pi))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> The value of PI is approximately 3.142.<\/p>\n\n\n\n<p>\u5728\u00a0<code>':'<\/code>\u00a0\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.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>table = {\u2018Sjoerd\u2019: 4127, \u2018Jack\u2019: 4098, \u2018Dcab\u2019: 7678}\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0for<\/strong>\u00a0name, phone\u00a0<strong>in<\/strong>\u00a0table.items():\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018<em>{0:10}<\/em>\u00a0==>\u00a0<em>{1:10d}<\/em>\u2018.format(name, phone))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Jack ==> 4098<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Dcab ==> 7678<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Sjoerd ==> 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\u00a0<code>'[]'<\/code>\u00a0\u6765\u8bbf\u95ee\u952e\u503c :<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>table = {\u2018Sjoerd\u2019: 4127, \u2018Jack\u2019: 4098, \u2018Dcab\u2019: 8637678}\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018Jack:\u00a0<em>{0[Jack]:d}<\/em>; Sjoerd:\u00a0<em>{0[Sjoerd]:d}<\/em>; \u2018 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2018Dcab: {0[Dcab]:d}\u2019.format(table)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">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.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>table = {\u2018Sjoerd\u2019: 4127, \u2018Jack\u2019: 4098, \u2018Dcab\u2019: 8637678}\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018Jack:\u00a0<em>{Jack:d}<\/em>; Sjoerd:\u00a0<em>{Sjoerd:d}<\/em>; Dcab:\u00a0<em>{Dcab:d}<\/em>\u2018.format(**table)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">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>\u00a0\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\u00a0<code>sprintf()<\/code>\u00a0\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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importmath<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>print(\u2018The value of PI is approximately\u00a0<em>%5.3f<\/em>.\u2019 % math.pi)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 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>\u00a0\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u00a0<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:\u00a0<code>open(filename,\u00a0mode)<\/code>.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\" id=\"index-1\"><strong>>>>\u00a0<\/strong>f = open(\u2018\/tmp\/workfile\u2019, \u2018w\u2019)<\/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\u00a0<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.\u00a0<em>size<\/em>\u00a0\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6570\u5b57\u7c7b\u578b\u7684\u53c2\u6570. \u5f53\u00a0<em>size<\/em>\u00a0\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\u00a0<em>size<\/em>\u00a0\u5b57\u8282\u5c06\u88ab\u8bfb\u53d6\u5e76\u8fd4\u56de. \u5982\u679c\u5230\u8fbe\u4e86\u6587\u4ef6\u7684\u672b\u5c3e,\u00a0<code>f.read()<\/code>\u00a0\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32 (<code>''<\/code>).<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.read() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2018This is the entire file.\\n\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>f.read() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u201d<\/p>\n\n\n\n<p><code>f.readline()<\/code>\u00a0\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;\u00a0<code>f.readline()<\/code>\u00a0\u5982\u679c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32, \u90a3\u4e48\u6587\u4ef6\u5df2\u5230\u5e95\u4e86, \u800c\u5982\u679c\u662f\u4ee5\u00a0<code>'\\n'<\/code>\u00a0\u8868\u793a, \u90a3\u4e48\u5c31\u662f\u53ea\u5305\u884c\u4e00\u4e2a\u65b0\u884c.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.readline()<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2018This is the first line of the file.\\n\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.readline() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2018Second line of the file\\n\u2019<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>f.readline()<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u201d<\/p>\n\n\n\n<p><code>f.readlines()<\/code>\u00a0\u5c06\u8fd4\u56de\u8be5\u6587\u4ef6\u4e2d\u5305\u542b\u7684\u6240\u6709\u884c. \u5982\u679c\u7ed9\u5b9a\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\u00a0<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.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.readlines()<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [\u2018This is the first line of the file.\\n\u2019, \u2018Second line of the file\\n\u2019]<\/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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0for<\/strong>\u00a0line\u00a0<strong>in<\/strong>\u00a0f:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(line, end=\u201d)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">This is the first line of the file.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 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>\u00a0\u5c06\u00a0<em>string<\/em>\u00a0\u5199\u5165\u5230\u6587\u4ef6\u4e2d, \u7136\u540e\u8fd4\u56de\u5199\u5165\u7684\u5b57\u7b26\u6570.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.write(\u2018This is a test<strong>\\n<\/strong>\u2018)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 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:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>value = (\u2018the answer\u2019, 42)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>s = str(value)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.write(s) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">18<\/p>\n\n\n\n<p><code>f.tell()<\/code>\u00a0\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\u00a0<code>f.seek(offset,\u00a0from_what)<\/code>. \u8fd9\u4e2a\u4f4d\u7f6e\u662f\u901a\u8fc7\u5c06\u5f53\u524d\u4f4d\u7f6e\u52a0\u4e0a\u00a0<em>offset<\/em>\u00a0\u6240\u5f97.\u00a0<em>from_what<\/em>\u00a0\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.\u00a0<em>from_what<\/em>\u00a0\u7684\u9ed8\u8ba4\u4e3a 0, \u5373\u4ece\u5f00\u5934\u5f00\u59cb.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f = open(\u2018\/tmp\/workfile\u2019, \u2018rb+\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.write(b\u20190123456789abcdef\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">16\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.seek(5)\u00a0<em># Go to the 6th byte in the file<\/em>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">5<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>f.read(1) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">b\u20195\u2032\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.seek(-3, 2)\u00a0<em># Go to the 3rd byte before the end<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a013<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>f.read(1)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> b\u2019d\u2019<\/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\u00a0<code>f.close()<\/code>\u00a0\u4f1a\u5173\u95ed\u5b83, \u5e76\u91ca\u653e\u7cfb\u7edf\u7684\u8d44\u6e90. \u5728\u8c03\u7528\u5b8c\u00a0<code>f.close()<\/code>\u00a0\u4e4b\u540e, \u5c1d\u8bd5\u4f7f\u7528\u90a3\u4e2a\u6587\u4ef6\u5bf9\u8c61\u662f\u4f1a\u5931\u8d25\u7684.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.close()\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.read() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 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\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#with\"><code>with<\/code><\/a>\u00a0\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\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u2013\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>\u00a0\u8bed\u53e5\u5757\u8981\u7b80\u77ed:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0with<\/strong>\u00a0open(\u2018\/tmp\/workfile\u2019, \u2018r\u2019)\u00a0<strong>as<\/strong>\u00a0f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0read_data = f.read()\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>f.closed <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">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\u00a0<code>x<\/code>, \u548c\u4e00\u4e2a\u5df2\u7ecf\u6253\u5f00\u5e76\u7528\u4e8e\u5199\u7684\u6587\u4ef6\u5bf9\u8c61\u00a0<code>f<\/code>, pickle \u8fd9\u4e2a\u5bf9\u8c61\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">pickle.dump(x, f)<\/p>\n\n\n\n<p>\u6709\u4e86 pickle \u8fd9\u4e2a\u5bf9\u8c61, \u5c31\u80fd\u5bf9\u00a0<code>f<\/code>\u00a0\u4ee5\u8bfb\u53d6\u7684\u5f62\u5f0f\u6253\u5f00:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">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>\u00a0\u662f Python \u4e2d\u4fdd\u5b58\u53ca\u91cd\u7528\u5bf9\u8c61\u7684\u6807\u51c6\u65b9\u5f0f; \u6807\u51c6\u7684\u5c5e\u4e8e\u79f0\u4e3a\u00a0<em>persistent<\/em>\u00a0\u5bf9\u8c61 (\u5373\u6301\u4e45\u5316\u5bf9\u8c61). \u56e0\u4e3a\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pickle.html#module-pickle\"><code>pickle<\/code><\/a>\u00a0\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-273","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\/273","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=273"}],"version-history":[{"count":9,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":379,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/273\/revisions\/379"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}