{"id":999455,"date":"2024-12-27T09:41:38","date_gmt":"2024-12-27T01:41:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999455.html"},"modified":"2024-12-27T09:41:40","modified_gmt":"2024-12-27T01:41:40","slug":"python%e5%a6%82%e4%bd%95%e6%a0%bc%e5%bc%8f%e5%86%99%e5%85%a5%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999455.html","title":{"rendered":"Python\u5982\u4f55\u683c\u5f0f\u5199\u5165\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074443\/29c82e82-b001-4016-bfc8-b6b5bdc53bb1.webp\" alt=\"Python\u5982\u4f55\u683c\u5f0f\u5199\u5165\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u683c\u5f0f\u5316\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-string\u3001\u4ee5\u53cawrite\u51fd\u6570\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6570\u636e\u4ee5\u7279\u5b9a\u7684\u683c\u5f0f\u5199\u5165\u6587\u4ef6\u3001f-string\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u9ad8\u6548\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3001\u4f7f\u7528write\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528f-string\u8fdb\u884c\u683c\u5f0f\u5316\u5199\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528f-string\u8fdb\u884c\u683c\u5f0f\u5316\u5199\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u52a8\u6001\u683c\u5f0f\u5316\u3002f-string\u662f\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u4ecePython 3.6\u5f00\u59cb\u5f15\u5165\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u76f4\u89c2\u3001\u7b80\u6d01\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002f-string\u4ee5\u5b57\u6bcd<code>f<\/code>\u6216<code>F<\/code>\u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u8868\u8fbe\u5f0f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u5305\u56f4\u3002\u5728f-string\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5d4c\u5165\u53d8\u91cf\u540d\u3001\u8868\u8fbe\u5f0f\u751a\u81f3\u662f\u51fd\u6570\u8c03\u7528\uff0cPython\u4f1a\u81ea\u52a8\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u503c\u5e76\u683c\u5f0f\u5316\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6587\u4ef6\u5199\u5165\u7684\u57fa\u672c\u65b9\u6cd5  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u7684\u57fa\u672c\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>write()<\/code>\u6216<code>writelines()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3002\u8be5\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6a21\u5f0f\u3002\u6a21\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>\uff1a\u53ea\u8bfb\u6a21\u5f0f\u3002<\/li>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\u3002\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002<\/li>\n<li><code>&#39;b&#39;<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f\u3002<\/li>\n<li><code>&#39;t&#39;<\/code>\uff1a\u6587\u672c\u6a21\u5f0f\uff08\u9ed8\u8ba4\uff09\u3002<\/li>\n<li><code>&#39;x&#39;<\/code>\uff1a\u72ec\u5360\u521b\u5efa\u6a21\u5f0f\uff0c\u5982\u679c\u6587\u4ef6\u5b58\u5728\u5219\u5931\u8d25\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u8981\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5199\u5165\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>\u4e00\u65e6\u6587\u4ef6\u6253\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002\u6ce8\u610f\uff0c<code>write()<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\u3002\u5982\u679c\u9700\u8981\u6362\u884c\uff0c\u9700\u8981\u624b\u52a8\u6dfb\u52a0<code>\\n<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>file.write(&#39;This is a new line.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u5173\u95ed\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u5199\u5165\u5b8c\u6210\u540e\uff0c\u9700\u8981\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u88ab\u4fdd\u5b58\u5230\u78c1\u76d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5199\u5165  <\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5176\u4e2d\u5305\u62ec\u65e7\u5f0f\u683c\u5f0f\u5316\u3001<code>str.format()<\/code>\u65b9\u6cd5\u548cf-string\u3002\u6211\u4eec\u5728\u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecd<code>str.format()<\/code>\u548cf-string\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>str.format()<\/code><\/li>\n<\/ol>\n<p><p><code>str.format()<\/code>\u662f\u8f83\u4e3a\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5360\u4f4d\uff0c\u7136\u540e\u5728<code>format()<\/code>\u65b9\u6cd5\u4e2d\u63d0\u4f9b\u66ff\u6362\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>age = 30<\/p>\n<p>file.write(&#39;Name: {}\\nAge: {}\\n&#39;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528f-string<\/li>\n<\/ol>\n<p><p>f-string\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0<code>f<\/code>\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5f15\u7528\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(f&#39;Name: {name}\\nAge: {age}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5199\u5165\u591a\u884c\u6570\u636e  <\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5199\u5165\u591a\u884c\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>writelines()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u7b26\u4e32\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;First line\\n&#39;, &#39;Second line\\n&#39;, &#39;Third line\\n&#39;]<\/p>\n<p>file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>with<\/code>\u6765\u7ba1\u7406\u6587\u4ef6\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u5373\u4f7f\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a new line.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u683c\u5f0f\u5316\u6570\u503c\u548c\u5b57\u7b26\u4e32  <\/p>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u683c\u5f0f\u5316\u6570\u503c\u548c\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fdd\u8bc1\u8f93\u51fa\u7684\u7f8e\u89c2\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\u683c\u5f0f\u5316\u6570\u503c<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5728f-string\u4e2d\u4f7f\u7528\u683c\u5f0f\u8bf4\u660e\u7b26\u6765\u683c\u5f0f\u5316\u6570\u503c\u3002\u4f8b\u5982\uff0c\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pi = 3.1415926<\/p>\n<p>file.write(f&#39;Pi: {pi:.2f}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5de6\u5bf9\u9f50\u3001\u53f3\u5bf9\u9f50\u548c\u5c45\u4e2d\u5bf9\u9f50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &#39;Alice&#39;<\/p>\n<p>file.write(f&#39;|{name:&lt;10}|\\n&#39;)  # \u5de6\u5bf9\u9f50<\/p>\n<p>file.write(f&#39;|{name:&gt;10}|\\n&#39;)  # \u53f3\u5bf9\u9f50<\/p>\n<p>file.write(f&#39;|{name:^10}|\\n&#39;)  # \u5c45\u4e2d\u5bf9\u9f50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6  <\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u5185\u5b58\u6ea2\u51fa\uff0c\u53ef\u4ee5\u9009\u62e9\u9010\u884c\u5199\u5165\u6216\u4f7f\u7528\u7f13\u51b2\u533a\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9010\u884c\u5199\u5165<\/li>\n<\/ol>\n<p><p>\u9010\u884c\u5199\u5165\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6309\u884c\u5904\u7406\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for line in data_source:<\/p>\n<p>        file.write(line + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u7f13\u51b2\u533a<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u63d0\u9ad8\u5199\u5165\u6548\u7387\u3002<code>open()<\/code>\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570<code>buffering<\/code>\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u7f13\u51b2\u533a\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;w&#39;, buffering=2048) as file:<\/p>\n<p>    file.write(&#39;Some large data...\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u9519\u8bef\u5904\u7406  <\/p>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;readonly.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Trying to write...\\n&#39;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&#39;An IOError occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u683c\u5f0f\u5316\u5199\u5165\u6587\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u548c\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u8f93\u51fa\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u90fd\u5c06\u5927\u5927\u63d0\u9ad8\u60a8\u7684\u7f16\u7a0b\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ee5\u7279\u5b9a\u683c\u5f0f\u5199\u5165\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7ed3\u5408\u6587\u4ef6\u6a21\u5f0f\uff08\u5982<code>&#39;w&#39;<\/code>\u8868\u793a\u5199\u5165\u3001<code>&#39;a&#39;<\/code>\u8868\u793a\u8ffd\u52a0\u7b49\uff09\u6765\u6253\u5f00\u6587\u4ef6\u3002\u4e3a\u4e86\u683c\u5f0f\u5316\u5199\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>f-strings<\/code>\u3001<code>str.format()<\/code>\u6216<code>%<\/code>\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>f-strings<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d2\u5165\u53d8\u91cf\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u7136\u540e\u5c06\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u54ea\u4e9b\u6587\u4ef6\u683c\u5f0f\u652f\u6301Python\u7684\u5199\u5165\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\u7684\u5199\u5165\uff0c\u5305\u62ec\u6587\u672c\u6587\u4ef6\uff08\u5982<code>.txt<\/code>\uff09\u3001CSV\u6587\u4ef6\uff08<code>.csv<\/code>\uff09\u3001JSON\u6587\u4ef6\uff08<code>.json<\/code>\uff09\u3001Excel\u6587\u4ef6\uff08<code>.xlsx<\/code>\uff09\u7b49\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\uff0c\u4f8b\u5982\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u5904\u7406CSV\u6587\u4ef6\uff0c\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5904\u7406JSON\u6587\u4ef6\uff0c\u6216\u4f7f\u7528<code>pandas<\/code>\u5e93\u5904\u7406Excel\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5199\u5165\u6587\u4ef6\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u672a\u627e\u5230\u3001\u6743\u9650\u4e0d\u8db3\u6216\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u901a\u8fc7\u6355\u83b7<code>IOError<\/code>\u6216<code>OSError<\/code>\uff0c\u53ef\u4ee5\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u5199\u5165\u5931\u8d25\u800c\u5d29\u6e83\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\uff0c\u6bd4\u5982\u91cd\u65b0\u5c1d\u8bd5\u5199\u5165\u6216\u901a\u77e5\u7528\u6237\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5199\u5165\u7684\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff1f<\/strong><br \/>\u4e3a\u786e\u4fdd\u5199\u5165\u7684\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff0c\u53ef\u4ee5\u5728\u5199\u5165\u4e4b\u524d\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u3002\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3001\u957f\u5ea6\u6216\u7279\u5b9a\u683c\u5f0f\u8981\u6c42\uff0c\u4f8b\u5982\u786e\u4fdd\u65e5\u671f\u683c\u5f0f\u4e3a<code>YYYY-MM-DD<\/code>\u3002\u53e6\u5916\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u9a8c\u8bc1\u8868\u683c\u6570\u636e\uff0c\u786e\u4fdd\u6570\u636e\u5728\u5199\u5165\u524d\u7b26\u5408\u9884\u671f\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u683c\u5f0f\u5316\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-string\u3001\u4ee5\u53cawrite\u51fd [&hellip;]","protected":false},"author":3,"featured_media":999458,"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\/999455"}],"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=999455"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999455\/revisions"}],"predecessor-version":[{"id":999460,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999455\/revisions\/999460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999458"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}