{"id":344563,"date":"2021-02-13T15:14:34","date_gmt":"2021-02-13T07:14:34","guid":{"rendered":"http:\/\/4563.org\/?p=344563"},"modified":"2021-02-13T15:14:34","modified_gmt":"2021-02-13T07:14:34","slug":"%e7%bf%bb%e8%af%91%ef%bc%9a%e3%80%8a%e5%ae%9e%e7%94%a8%e7%9a%84-python-%e7%bc%96%e7%a8%8b%e3%80%8b01_06_files","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344563","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b01_06_Files"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b01_06_Files               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>1.6 \u6587\u4ef6\u7ba1\u7406<\/h1>\n<p> <\/p>\n<p>\u5927\u591a\u6570\u7684\u7a0b\u5e8f\u9700\u8981\u4ece\u67d0\u5904\u8bfb\u53d6\u8f93\u5165\u3002\u672c\u8282\u8ba8\u8bba\u6587\u4ef6\u8bbf\u95ee\u3002<\/p>\n<h3>\u6587\u4ef6\u8f93\u5165\u548c\u8f93\u51fa<\/h3>\n<p>\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff1a<\/p>\n<pre><code>f = open('foo.txt', 'rt')     # Open for reading (text) g = open('bar.txt', 'wt')     # Open for writing (text) <\/code><\/pre>\n<p>\u8bfb\u53d6\u6240\u6709\u7684\u6570\u636e\uff1a<\/p>\n<pre><code>data = f.read()  # Read only up to 'maxbytes' bytes data = f.read([maxbytes]) <\/code><\/pre>\n<p>\u5199\u5165\u4e00\u4e9b\u6587\u672c\uff1a<\/p>\n<pre><code>g.write('some text') <\/code><\/pre>\n<p>\u5f53\u4f60\u5b8c\u6210\u64cd\u4f5c\u540e\uff0c\u5173\u95ed\u6587\u4ef6\uff1a<\/p>\n<pre><code>f.close() g.close() <\/code><\/pre>\n<p>\u6587\u4ef6\u5e94\u8be5\u88ab\u6b63\u786e\u5730\u5173\u95ed\uff0c\u8fd9\u662f\u5f88\u5bb9\u6613\u5fd8\u8bb0\u7684\u4e00\u4e2a\u6b65\u9aa4\u3002\u56e0\u6b64\uff0c\u9996\u9009\u65b9\u6cd5\u662f\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\uff1a<\/p>\n<pre><code>with open(filename, 'rt') as file:     # Use the file `file`     ...     # No need to close explicitly ...statements <\/code><\/pre>\n<p>\u5f53\u63a7\u5236\u6d41\u79bb\u5f00\u7f29\u8fdb\u7684\u4ee3\u7801\u5757\u65f6\uff0c\u8fd9\u5c06\u4f1a\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<h3>\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u7684\u4e60\u60ef\u7528\u6cd5<\/h3>\n<p>\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff1a<\/p>\n<pre><code>with open('foo.txt', 'rt') as file:     data = file.read()     # `data` is a string with all the text in `foo.txt` <\/code><\/pre>\n<p>\u901a\u8fc7\u8fed\u4ee3\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff1a<\/p>\n<pre><code>with open(filename, 'rt') as file:     for line in file:         # Process the line <\/code><\/pre>\n<h3>\u5199\u5165\u6587\u4ef6\u7684\u4e60\u60ef\u7528\u6cd5<\/h3>\n<p>\u5199\u5165\u5b57\u7b26\u4e32\u6570\u636e\uff1a<\/p>\n<pre><code>with open('outfile', 'wt') as out:     out.write('Hello Worldn')     ... <\/code><\/pre>\n<p>\u91cd\u5b9a\u5411 print() \u51fd\u6570\uff1a<\/p>\n<pre><code>with open('outfile', 'wt') as out:     print('Hello World', file=out)     ... <\/code><\/pre>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u672c\u7ec3\u4e60\u4f9d\u8d56\u4e8e <code>Data\/portfolio.csv<\/code> \u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u7531\u4e00\u884c\u4e00\u884c\u7684\u5173\u4e8e\u80a1\u7968\u6295\u8d44\u7ec4\u5408\u7684\u4fe1\u606f\u6784\u6210\u3002\u5047\u5b9a\u4f60\u662f\u5728 <code>practical-python\/Work\/<\/code> \u76ee\u5f55\u4e0b\u8fdb\u884c\u64cd\u4f5c\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u81ea\u5df1\u6240\u5728\u7684\u76ee\u5f55\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u627e\u51fa Python \u8fd0\u884c\u7684\u76ee\u5f55\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; os.getcwd() '\/Users\/beazley\/Desktop\/practical-python\/Work' # Output vary &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.26: \u6587\u4ef6\u9884\u89c8<\/h3>\n<p>\u9996\u5148\uff0c\u5c1d\u8bd5\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; with open('Data\/portfolio.csv', 'rt') as f:         data = f.read()  &gt;&gt;&gt; data 'name,shares,pricen\"AA\",100,32.20n\"IBM\",50,91.10n\"CAT\",150,83.44n\"MSFT\",200,51.23n\"GE\",95,40.37n\"MSFT\",50,65.10n\"IBM\",100,70.44n' &gt;&gt;&gt; print(data) name,shares,price \"AA\",100,32.20 \"IBM\",50,91.10 \"CAT\",150,83.44 \"MSFT\",200,51.23 \"GE\",95,40.37 \"MSFT\",50,65.10 \"IBM\",100,70.44 &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5e94\u6ce8\u610f Python \u6709\u4e24\u79cd\u8f93\u51fa\u6a21\u5f0f\u3002\u5728\u7b2c\u4e00\u79cd\u6a21\u5f0f\u4e0b\uff0c\u4f60\u5728\u63d0\u793a\u7b26\u540e\u8f93\u5165 <code>data<\/code>\uff0cPython \u5411\u4f60\u5c55\u793a\u5305\u542b\u5f15\u53f7\u548c\u8f6c\u4e49\u7801\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u5f53\u4f60\u8f93\u5165 <code>print(data)<\/code>\u65f6\uff0c\u4f60\u83b7\u5f97\u771f\u6b63\u7684\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p>\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u5f88\u7b80\u5355\uff0c\u4f46\u8fd9\u901a\u5e38\u4e0d\u662f\u6700\u6070\u5f53\u7684\u8bfb\u53d6\u6587\u4ef6\u65b9\u5f0f\u2014\u2014\u5c24\u5176\u662f\u5f53\u6587\u4ef6\u78b0\u5de7\u5f88\u5927\u6216\u8005\u6587\u4ef6\u5305\u542b\u8981\u4e00\u6b21\u6027\u5904\u7406\u7684\u6587\u672c\u884c\u65f6\u3002<\/p>\n<p>\u8981\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 for \u5faa\u73af\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; with open('Data\/portfolio.csv', 'rt') as f:         for line in f:             print(line, end='')  name,shares,price \"AA\",100,32.20 \"IBM\",50,91.10 ... &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5f53\u4f60\u50cf\u4e0a\u9762\u5c55\u793a\u7684\u90a3\u6837\u4f7f\u7528\u6b64\u4ee3\u7801\u65f6\uff0c\u5b83\u4f1a\u8bfb\u53d6\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\uff0c\u76f4\u5230\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\u3002\u5728\u6587\u4ef6\u672b\u5c3e\uff0c\u5faa\u73af\u4f1a\u505c\u6b62\u3002<\/p>\n<p>\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u624b\u52a8\u7684\u8bfb\u53d6\u6216\u8005\u8df3\u8fc7\u67d0\u4e00\u884c\u6587\u672c\uff08\u4f8b\u5982\uff0c\u53ef\u80fd\u4f60\u60f3\u8df3\u8fc7\u5217\u6807\u9898\u7684\u7b2c\u4e00\u884c\uff09\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; f = open('Data\/portfolio.csv', 'rt') &gt;&gt;&gt; headers = next(f) &gt;&gt;&gt; headers 'name,shares,pricen' &gt;&gt;&gt; for line in f:     print(line, end='')  \"AA\",100,32.20 \"IBM\",50,91.10 ... &gt;&gt;&gt; f.close() &gt;&gt;&gt; <\/code><\/pre>\n<p><code>next()<\/code> \u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u4e2d\u7684\u4e0b\u4e00\u884c\u6587\u672c\u3002\u5982\u679c\u4f60\u53cd\u590d\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5c06\u4f1a\u83b7\u5f97\u8fde\u7eed\u7684\u884c\u3002\u4f46\u662f\uff0c\u5982\u4f60\u6240\u77e5\uff0c<code>for<\/code> \u5faa\u73af\u5df2\u7ecf\u4f7f\u7528 <code>next()<\/code> \u51fd\u6570\u83b7\u53d6\u6570\u636e\u4e86\u3002\u6240\u4ee5\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u9664\u975e\u4f60\u50cf\u4e0a\u9762\u5c55\u793a\u7684\u90a3\u6837\u8bd5\u56fe\u663e\u5f0f\u8df3\u8fc7\u6216\u8005\u8bfb\u53d6\u4e00\u884c\uff0c\u5426\u5219\uff0c\u4e0d\u8981\u76f4\u63a5\u8c03\u7528 <code>next()<\/code> \u51fd\u6570\u3002<\/p>\n<p>\u4e00\u65e6\u8bfb\u53d6\u6587\u4ef6\u7684\u5404\u884c\uff0c\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u6267\u884c\u66f4\u591a\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u62c6\u5206\u3002<\/p>\n<p>\u793a\u4f8b\uff0c\u5c1d\u8bd5\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; f = open('Data\/portfolio.csv', 'rt') &gt;&gt;&gt; headers = next(f).split(',') &gt;&gt;&gt; headers ['name', 'shares', 'pricen'] &gt;&gt;&gt; for line in f:     row = line.split(',')     print(row)  ['\"AA\"', '100', '32.20n'] ['\"IBM\"', '50', '91.10n'] ... &gt;&gt;&gt; f.close() <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u6ca1\u6709\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\uff0c\u6240\u4ee5 <code>f.close()<\/code> \u88ab\u663e\u5f0f\u7684\u8c03\u7528\u3002<\/p>\n<h3>\u7ec3\u4e60 1.27: \u8bfb\u53d6\u6570\u636e\u6587\u4ef6<\/h3>\n<p>\u73b0\u5728\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\uff0c\u8ba9\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u6267\u884c\u7b80\u5355\u7684\u8ba1\u7b97\u3002<\/p>\n<p><code>portfolio.csv<\/code> \u6587\u4ef6\u91cc\u9762\u7684\u5217\u5bf9\u5e94\u80a1\u7968\u7684\u540d\u79f0\uff0c\u80a1\u7968\u7684\u6570\u91cf\u4ee5\u53ca\u5355\u652f\u80a1\u7968\u6301\u6709\u7684\u8d2d\u4e70\u4ef7\u683c\u3002\u7f16\u5199\u4e00\u4e2a\u540d\u4e3a <code>pcost.py<\/code> \u7684\u7a0b\u5e8f\u6765\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u6240\u6709\u7684\u884c\u5e76\u4e14\u8ba1\u7b97\u5728\u6240\u6709\u7684\u80a1\u7968\u6295\u8d44\u7ec4\u5408\u4e2d\u82b1\u8d39\u591a\u5c11\u94b1\u6765\u8d2d\u4e70\u8fd9\u4e9b\u80a1\u4efd\u3002<\/p>\n<p>\u63d0\u793a\uff1a\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\uff0c\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6d6e\u70b9\u6570\uff0c\u4f7f\u7528 <code>float()<\/code> \u51fd\u6570\u3002<\/p>\n<p>\u4f60\u7684\u7a0b\u5e8f\u5e94\u8be5\u6253\u5370\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre><code>Total cost 44671.15 <\/code><\/pre>\n<h3>\u7ec3\u4e60 1.28: \u5176\u5b83\u7c7b\u578b\u7684\u201c\u6587\u4ef6&#8221;<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u8bfb\u53d6\u975e\u6587\u672c\u6587\u4ef6\uff08\u5982 gzip \u538b\u7f29\u7684\u6570\u636e\u6587\u4ef6\uff09\u600e\u4e48\u529e\uff1f\u867d\u7136\u5185\u5efa <code>open()<\/code> \u51fd\u6570\u5728\u8fd9\u91cc\u5e2e\u4e0d\u4e86\u4f60\u4e86\uff0c\u4f46\u662f Python \u63d0\u4f9b\u7684 <code>gzip<\/code> \u6a21\u5757\u53ef\u4ee5\u8bfb\u53d6 gzip \u6587\u4ef6\u3002<\/p>\n<p>\u8bd5\u8bd5\u770b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; import gzip &gt;&gt;&gt; with gzip.open('Data\/portfolio.csv.gz', 'rt') as f:     for line in f:         print(line, end='')  ... look at the output ... &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u6b64\u5904\u5305\u542b <code>'rt'<\/code> \u6587\u4ef6\u6a21\u5f0f\u81f3\u5173\u91cd\u8981\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u4f7f\u7528\u5b83\uff0c\u4f60\u5c06\u4f1a\u5f97\u5230\u5b57\u8282\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u901a\u5e38\u7684\u6587\u672c\u5b57\u7b26\u4e32\u3002<\/p>\n<h3>\u8bf4\u660e: \u6211\u4eec\u4e0d\u5e94\u8be5\u4e3a\u6b64\u4f7f\u7528 Pandas \u5417?<\/h3>\n<p>\u6570\u636e\u79d1\u5b66\u5bb6\u5f88\u5feb\u6307\u51fa\uff0c\u8bf8\u5982 Pandas \u8fd9\u6837\u7684\u5e93\u5df2\u7ecf\u5177\u6709\u8bfb\u53d6 CSV \u6587\u4ef6\u7684\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 Pandas \u5462?\u7684\u786e\u5982\u6b64\u2014\u2014\u800c\u4e14\u6548\u679c\u4e5f\u5f88\u597d\u3002\u4f46\u662f\uff0c\u8fd9\u4e0d\u662f\u4e00\u95e8\u6709\u5173\u5b66\u4e60 Pandas \u7684\u8bfe\u7a0b\uff0c\u8bfb\u53d6\u6587\u4ef6\u662f\u4e00\u4e2a\u6bd4\u8bfb\u53d6 CSV \u6587\u4ef6\u66f4\u666e\u904d\u7684\u95ee\u9898\u3002\u6211\u4eec\u4f7f\u7528 CSV \u6587\u4ef6\u505a\u4f8b\u5b50\u7684\u4e3b\u8981\u539f\u56e0\u662f\uff1a\u5b83\u662f\u5927\u591a\u6570\u7f16\u7801\u4eba\u5458\u719f\u6089\u7684\u683c\u5f0f\uff0c\u5e76\u4e14\u76f8\u5bf9\u6613\u4e8e\u76f4\u63a5\u4f7f\u7528\u2014\u2014\u5728\u8bfb\u53d6 CSV \u6587\u4ef6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9610\u660e\u4e86\u8bb8\u591a Python \u7279\u6027\u3002\u6240\u4ee5\uff0c\u5f53\u4f60\u56de\u5230\u5de5\u4f5c\u4e2d\uff0c\u8bf7\u52a1\u5fc5\u4f7f\u7528 Pandas \u3002\u4f46\u662f\uff0c\u5728\u672c\u8bfe\u7a0b\u5269\u4e0b\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4f1a\u7ee7\u7eed\u4f7f\u7528\u6807\u51c6\u7684 Python \u51fd\u6570\u3002<\/p>\n<p>\u6ce8\uff1a\u5b8c\u6574\u7ffb\u8bd1\u89c1 https:\/\/github.com\/codists\/practical-python-zh<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344563"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344563"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344563\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344563"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}