{"id":360932,"date":"2021-02-20T17:43:01","date_gmt":"2021-02-20T09:43:01","guid":{"rendered":"http:\/\/4563.org\/?p=360932"},"modified":"2021-02-20T17:43:01","modified_gmt":"2021-02-20T09:43:01","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%8b03_02_more_functions","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=360932","title":{"rendered":"\u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b03_02_More_functions"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7ffb\u8bd1\uff1a\u300a\u5b9e\u7528\u7684 Python \u7f16\u7a0b\u300b03_02_More_functions               <\/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>3.2 \u6df1\u5165\u51fd\u6570<\/h1>\n<p> <\/p>\n<p>\u5c3d\u7ba1\u51fd\u6570\u5728\u65e9\u5148\u65f6\u5019\u4ecb\u7ecd\u4e86\uff0c\u4f46\u6709\u5173\u51fd\u6570\u5728\u66f4\u6df1\u5c42\u6b21\u4e0a\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u7ec6\u8282\u5374\u5f88\u5c11\u63d0\u4f9b\u3002\u672c\u8282\u65e8\u5728\u586b\u8865\u8fd9\u4e9b\u7a7a\u767d\uff0c\u5e76\u8ba8\u8bba\u51fd\u6570\u8c03\u7528\u7ea6\u5b9a\uff0c\u4f5c\u7528\u57df\u89c4\u5219\u7b49\u95ee\u9898\u3002<\/p>\n<h3>\u8c03\u7528\u51fd\u6570<\/h3>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre><code>def read_prices(filename, debug):     ... <\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n<pre><code>prices = read_prices('prices.csv', True) <\/code><\/pre>\n<p>\u6216\u8005\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n<pre><code>prices = read_prices(filename='prices.csv', debug=True) <\/code><\/pre>\n<h3>\u9ed8\u8ba4\u53c2\u6570<\/h3>\n<p>\u6709\u65f6\u5019\uff0c\u4f60\u5e0c\u671b\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u662f\u8fd9\u6837\uff0c\u8bf7\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u5206\u914d\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre><code>def read_prices(filename, debug=False):     ... <\/code><\/pre>\n<p>\u5982\u679c\u5206\u914d\u4e86\u9ed8\u8ba4\u503c\uff0c\u5219\u53c2\u6570\u5728\u51fd\u6570\u8c03\u7528\u4e2d\u662f\u53ef\u9009\u7684\u3002<\/p>\n<pre><code>d = read_prices('prices.csv') e = read_prices('prices.dat', True) <\/code><\/pre>\n<p><em>\u6ce8\u610f\uff1a\u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\uff08\u8bd1\u6ce8\uff1a\u5373\u5173\u952e\u5b57\u53c2\u6570\uff09\u5fc5\u987b\u51fa\u73b0\u5728\u53c2\u6570\u5217\u8868\u7684\u672b\u5c3e\uff08\u6240\u6709\u975e\u53ef\u9009\u53c2\u6570\u90fd\u653e\u5728\u6700\u524d\u9762\uff09<\/em><\/p>\n<h3>\u9996\u9009\u5173\u952e\u5b57\u53c2\u6570\u4f5c\u4e3a\u53ef\u9009\u53c2\u6570<\/h3>\n<p>\u6bd4\u8f83\u4ee5\u4e0b\u4e24\u79cd\u4e0d\u540c\u7684\u8c03\u7528\u98ce\u683c\uff1a<\/p>\n<pre><code>parse_data(data, False, True) # ?????  parse_data(data, ignore_errors=True) parse_data(data, debug=True) parse_data(data, debug=True, ignore_errors=True) <\/code><\/pre>\n<p>\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u5b57\u53c2\u6570\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u2014\u2014\u7279\u522b\u662f\u5bf9\u4e8e\u7528\u4f5c\u6807\u5fd7\u7684\u53c2\u6570\uff0c\u6216\u8005\u4e0e\u53ef\u9009\u7279\u6027\u76f8\u5173\u7684\u53c2\u6570\u3002<\/p>\n<h3>\u8bbe\u8ba1\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p>\u59cb\u7ec8\u4e3a\u51fd\u6570\u53c2\u6570\u6307\u5b9a\u7b80\u77ed\u4f46\u6709\u610f\u4e49\u7684\u540d\u79f0\u3002<\/p>\n<p>\u4f7f\u7528\u51fd\u6570\u7684\u4eba\u53ef\u80fd\u60f3\u8981\u4f7f\u7528\u5173\u952e\u5b57\u8c03\u7528\u98ce\u683c\u3002<\/p>\n<pre><code>d = read_prices('prices.csv', debug=True) <\/code><\/pre>\n<p>Python \u5f00\u53d1\u5de5\u5177\u5c06\u4f1a\u5728\u5e2e\u52a9\u529f\u80fd\u6216\u8005\u5e2e\u52a9\u6587\u6863\u4e2d\u663e\u793a\u8fd9\u4e9b\u540d\u79f0\u3002<\/p>\n<h3>\u8fd4\u56de\u503c<\/h3>\n<p><code>return<\/code> \u8bed\u53e5\u8fd4\u56de\u4e00\u4e2a\u503c\uff1a<\/p>\n<pre><code>def square(x):     return x * x <\/code><\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u7ed9\u51fa\u8fd4\u56de\u503c\u6216\u8005 <code>return<\/code> \u8bed\u53e5\u7f3a\u5931\uff0c\u90a3\u4e48\u8fd4\u56de <code>None<\/code>\uff1a<\/p>\n<pre><code>def bar(x):     statements     return  a = bar(4)      # a = None  # OR def foo(x):     statements  # No `return`  b = foo(4)      # b = None <\/code><\/pre>\n<h3>\u591a\u4e2a\u8fd4\u56de\u503c<\/h3>\n<p>\u51fd\u6570\u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f46\u662f\uff0c\u901a\u8fc7\u5c06\u8fd4\u56de\u503c\u653e\u5230\u5143\u7ec4\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\uff1a<\/p>\n<pre><code>def divide(a,b):     q = a \/\/ b      # Quotient     r = a % b       # Remainder     return q, r     # Return a tuple <\/code><\/pre>\n<p>\u7528\u4f8b\uff1a<\/p>\n<pre><code>x, y = divide(37,5) # x = 7, y = 2  x = divide(37, 5)   # x = (7, 2) <\/code><\/pre>\n<h3>\u53d8\u91cf\u4f5c\u7528\u57df<\/h3>\n<p>\u7a0b\u5e8f\u7ed9\u53d8\u91cf\u8d4b\u503c\uff1a<\/p>\n<pre><code>x = value # Global variable  def foo():     y = value # Local variable <\/code><\/pre>\n<p>\u53d8\u91cf\u8d4b\u503c\u53d1\u751f\u5728\u51fd\u6570\u7684\u5185\u90e8\u548c\u5916\u90e8\u3002\u5b9a\u4e49\u5728\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u662f\u201c\u5168\u5c40\u7684\u201d\u3002\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u662f\u201c\u5c40\u90e8\u7684\u201d\u3002<\/p>\n<h3>\u5c40\u90e8\u53d8\u91cf<\/h3>\n<p>\u5728\u51fd\u6570\u5185\u90e8\u8d4b\u503c\u7684\u53d8\u91cf\u662f\u79c1\u6709\u7684\u3002<\/p>\n<pre><code>def read_portfolio(filename):     portfolio = []     for line in open(filename):         fields = line.split(',')         s = (fields[0], int(fields[1]), float(fields[2]))         portfolio.append(s)     return portfolio <\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>filename<\/code>, <code>portfolio<\/code>, <code>line<\/code>, <code>fields<\/code> \u548c <code>s<\/code> \u662f\u5c40\u90e8\u53d8\u91cf\u3002\u5728\u51fd\u6570\u8c03\u7528\u4e4b\u540e\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5c06\u4e0d\u4f1a\u4fdd\u7559\u6216\u8005\u4e0d\u53ef\u8bbf\u95ee\u3002<\/p>\n<pre><code>&gt;&gt;&gt; stocks = read_portfolio('portfolio.csv') &gt;&gt;&gt; fields Traceback (most recent call last): File \"&lt;stdin&gt;\", line 1, in ? NameError: name 'fields' is not defined &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5c40\u90e8\u53d8\u91cf\u4e5f\u4e0d\u80fd\u4e0e\u5176\u5b83\u5730\u65b9\u7684\u53d8\u91cf\u51b2\u7a81\u3002<\/p>\n<h3>\u5168\u5c40\u53d8\u91cf<\/h3>\n<p>\u51fd\u6570\u53ef\u4ee5\u81ea\u7531\u5730\u8bbf\u95ee\u5b9a\u4e49\u5728\u540c\u4e00\u6587\u4ef6\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u503c\u3002<\/p>\n<pre><code>name = 'Dave'  def greeting():     print('Hello', name)  # Using `name` global variable <\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u51fd\u6570\u4e0d\u80fd\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<pre><code>name = 'Dave'  def spam():   name = 'Guido'  spam() print(name) # prints 'Dave' <\/code><\/pre>\n<p><strong>\u5207\u8bb0\uff1a\u51fd\u6570\u4e2d\u7684\u6240\u6709\u8d4b\u503c\u90fd\u662f\u5c40\u90e8\u7684<\/strong><\/p>\n<h3>\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/h3>\n<p>\u5982\u679c\u5fc5\u987b\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8bf7\u50cf\u4e0b\u9762\u8fd9\u6837\u58f0\u660e\u5b83\uff1a<\/p>\n<pre><code>name = 'Dave'  def spam():     global name     name = 'Guido' # Changes the global name above <\/code><\/pre>\n<p>\u5168\u5c40\u58f0\u660e\u5fc5\u987b\u5728\u4f7f\u7528\u4e4b\u524d\u51fa\u73b0\uff0c\u5e76\u4e14\u76f8\u5e94\u7684\u53d8\u91cf\u5fc5\u987b\u4e0e\u8be5\u51fd\u6570\u5904\u5728\u540c\u4e00\u6587\u4ef6\u4e2d\u3002\u770b\u4e0a\u9762\u8fd9\u4e2a\u51fd\u6570\uff0c\u8981\u77e5\u9053\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u5f62\u5f0f\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8bdd\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 <code>global<\/code> \u3002\u5982\u679c\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u4fee\u6539\u51fd\u6570\u5916\u90e8\u7684\u67d0\u79cd\u72b6\u6001\uff0c\u6700\u597d\u662f\u4f7f\u7528\u7c7b\u6765\u4ee3\u66ff\uff08\u7a0d\u540e\u8be6\u7ec6\u4ecb\u7ecd\uff09\u3002<\/p>\n<h3>\u53c2\u6570\u4f20\u9012<\/h3>\n<p>\u5f53\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u53c2\u6570\u53d8\u91cf\u7684\u4f20\u9012\u662f\u5f15\u7528\u4f20\u9012\u3002\u4e0d\u62f7\u8d1d\u503c\uff08\u53c2\u89c12.7 \u8282\uff09\u3002\u5982\u679c\u4f20\u9012\u4e86\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\uff0c\u5b57\u5178\uff09\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u539f\u5730\u4fee\u6539\u3002<\/p>\n<pre><code>def foo(items):     items.append(42)    # Modifies the input object  a = [1, 2, 3] foo(a) print(a)                # [1, 2, 3, 42] <\/code><\/pre>\n<p><strong>\u5173\u952e\u70b9\uff1a\u51fd\u6570\u4e0d\u63a5\u6536\u8f93\u5165\u53c2\u6570\u7684\u62f7\u8d1d\u3002<\/strong><\/p>\n<h3>\u91cd\u65b0\u8d4b\u503c\u4e0e\u4fee\u6539<\/h3>\n<p>\u786e\u4fdd\u4e86\u89e3\u4fee\u6539\u503c\u4e0e\u7ed9\u53d8\u91cf\u540d\u91cd\u65b0\u8d4b\u503c\u7684\u7ec6\u5fae\u5dee\u522b\u3002<\/p>\n<pre><code>def foo(items):     items.append(42)    # Modifies the input object  a = [1, 2, 3] foo(a) print(a)                # [1, 2, 3, 42]  # VS def bar(items):     items = [4,5,6]    # Changes local `items` variable to point to a different object  b = [1, 2, 3] bar(b) print(b)                # [1, 2, 3] <\/code><\/pre>\n<p><em>\u63d0\u9192\uff1a\u53d8\u91cf\u8d4b\u503c\u6c38\u8fdc\u4e0d\u4f1a\u91cd\u5199\u5185\u5b58\u3002\u540d\u79f0\u53ea\u662f\u88ab\u7ed1\u5b9a\u5230\u4e86\u65b0\u7684\u503c\u4e0a\u9762<\/em><\/p>\n<h2>\u7ec3\u4e60<\/h2>\n<p>\u672c\u7ec4\u7ec3\u4e60\u5b9e\u73b0\u7684\u5185\u5bb9\u53ef\u80fd\u662f\u672c\u8bfe\u7a0b\u6700\u5f3a\u5927\u7684\u548c\u6700\u96be\u7684\u3002\u6709\u5f88\u591a\u6b65\u9aa4\uff0c\u5e76\u4e14\u8fc7\u53bb\u7ec3\u4e60\u4e2d\u7684\u8bb8\u591a\u6982\u5ff5\u88ab\u4e00\u6b21\u6027\u6574\u5408\u5728\u4e00\u8d77\u3002\u867d\u7136\u6700\u540e\u7684\u9898\u89e3\u53ea\u6709\u5927\u7ea6 25 \u884c\u7684\u4ee3\u7801\uff0c\u4f46\u8981\u82b1\u70b9\u65f6\u95f4\uff0c\u786e\u4fdd\u4f60\u7406\u89e3\u6bcf\u4e00\u4e2a\u90e8\u5206\u3002<\/p>\n<p><code>report.py<\/code> \u7684\u4e2d\u5fc3\u90e8\u5206\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6 CSV \u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>read_portfolio()<\/code> \u51fd\u6570\u8bfb\u53d6\u5305\u542b\u6295\u8d44\u7ec4\u5408\u6570\u636e\u7684\u6587\u4ef6\uff0c<code>read_prices()<\/code> \u51fd\u6570\u8bfb\u53d6\u5305\u542b\u4ef7\u683c\u6570\u636e\u7684\u6587\u4ef6\u3002\u5728\u8fd9\u4e24\u4e2a\u51fd\u6570\u4e2d\uff0c\u6709\u5f88\u591a\u5e95\u5c42\u7684\u201c\u7cbe\u7ec6\u7684\u201d\u4e8b\u4ee5\u53ca\u76f8\u4f3c\u7684\u7279\u6027\u3002\u4f8b\u5982\uff0c\u5b83\u4eec\u90fd\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u6765\u5904\u7406\uff0c\u5e76\u4e14\u5c06\u5404\u79cd\u5b57\u6bb5\u8f6c\u6362\u4e3a\u65b0\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u771f\u7684\u9700\u8981\u5bf9\u5927\u91cf\u7684\u6587\u4ef6\u8fdb\u884c\u89e3\u6790\uff0c\u53ef\u80fd\u9700\u8981\u6e05\u7406\u5176\u4e2d\u7684\u4e00\u4e9b\u5185\u5bb9\u4f7f\u5176\u66f4\u901a\u7528\u3002\u8fd9\u662f\u6211\u4eec\u7684\u76ee\u6807\u3002<\/p>\n<p>\u901a\u8fc7\u6253\u5f00 <code>Work\/fileparse.py<\/code> \u6587\u4ef6\u5f00\u59cb\u672c\u7ec3\u4e60\uff0c\u8be5\u6587\u4ef6\u662f\u6211\u4eec\u5c06\u8981\u5199\u4ee3\u7801\u7684\u5730\u65b9\u3002<\/p>\n<h3>\u7ec3\u4e60 3.3\uff1a\u8bfb\u53d6 CSV \u6587\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u4ec5\u5173\u6ce8\u5c06 CSV \u6587\u4ef6\u8bfb\u5165\u5b57\u5178\u5217\u8868\u7684\u95ee\u9898\u3002\u5728 <code>fileparse.py<\/code> \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code># fileparse.py import csv  def parse_csv(filename):     '''     Parse a CSV file into a list of records     '''     with open(filename) as f:         rows = csv.reader(f)          # Read the file headers         headers = next(rows)         records = []         for row in rows:             if not row:    # Skip rows with no data                 continue             record = dict(zip(headers, row))             records.append(record)      return records <\/code><\/pre>\n<p>\u8be5\u51fd\u6570\u5c06 CSV \u6587\u4ef6\u8bfb\u5165\u5b57\u5178\u5217\u8868\u4e2d\uff0c\u4f46\u662f\u9690\u85cf\u4e86\u6253\u5f00\u6587\u4ef6\uff0c\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u5904\u7406\uff0c\u5ffd\u7565\u7a7a\u884c\u7b49\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u8bd5\u8bd5\u770b\uff1a<\/p>\n<p>\u63d0\u793a\uff1a <code>python3 -i fileparse.py<\/code>.<\/p>\n<pre><code>&gt;&gt;&gt; portfolio = parse_csv('Data\/portfolio.csv') &gt;&gt;&gt; portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u8fd9\u5f88\u597d\uff0c\u9664\u4e86\u4e0d\u80fd\u4f7f\u7528\u6570\u636e\u505a\u4efb\u4f55\u6709\u7528\u7684\u8ba1\u7b97\u4e4b\u5916\u3002\u56e0\u4e3a\u6240\u6709\u7684\u5185\u5bb9\u90fd\u662f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u3002\u6211\u4eec\u5c06\u9a6c\u4e0a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u5148\u8ba9\u6211\u4eec\u7ee7\u7eed\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u6784\u5efa\u3002<\/p>\n<h3>\u7ec3\u4e60 3.4\uff1a\u6784\u5efa\u5217\u9009\u62e9\u5668<\/h3>\n<p>\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ea\u5bf9 CSV \u6587\u4ef6\u4e2d\u9009\u5b9a\u7684\u5217\u611f\u5174\u8da3\uff0c\u800c\u4e0d\u662f\u6240\u6709\u6570\u636e\u3002\u4fee\u6539 <code>parse_csv()<\/code> \u51fd\u6570\uff0c\u4ee5\u4fbf\u8ba9\u7528\u6237\u6307\u5b9a\u4efb\u610f\u7684\u5217\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; # Read all of the data &gt;&gt;&gt; portfolio = parse_csv('Data\/portfolio.csv') &gt;&gt;&gt; portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}]  &gt;&gt;&gt; # Read only some of the data &gt;&gt;&gt; shares_held = parse_csv('Data\/portfolio.csv', select=['name','shares']) &gt;&gt;&gt; shares_held [{'name': 'AA', 'shares': '100'}, {'name': 'IBM', 'shares': '50'}, {'name': 'CAT', 'shares': '150'}, {'name': 'MSFT', 'shares': '200'}, {'name': 'GE', 'shares': '95'}, {'name': 'MSFT', 'shares': '50'}, {'name': 'IBM', 'shares': '100'}] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u7ec3\u4e60 2.23 \u4e2d\u7ed9\u51fa\u4e86\u5217\u9009\u62e9\u5668\u7684\u793a\u4f8b\u3002<\/p>\n<p>\u7136\u800c\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre><code># fileparse.py import csv  def parse_csv(filename, select=None):     '''     Parse a CSV file into a list of records     '''     with open(filename) as f:         rows = csv.reader(f)          # Read the file headers         headers = next(rows)          # If a column selector was given, find indices of the specified columns.         # Also narrow the set of headers used for resulting dictionaries         if select:             indices = [headers.index(colname) for colname in select]             headers = select         else:             indices = []          records = []         for row in rows:             if not row:    # Skip rows with no data                 continue             # Filter the row if specific columns were selected             if indices:                 row = [ row[index] for index in indices ]              # Make a dictionary             record = dict(zip(headers, row))             records.append(record)      return records <\/code><\/pre>\n<p>\u8fd9\u90e8\u5206\u6709\u4e00\u4e9b\u68d8\u624b\u7684\u95ee\u9898\uff0c\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u53ef\u80fd\u662f\u5217\u9009\u62e9\u5230\u884c\u7d22\u5f15\u7684\u6620\u5c04\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u8f93\u5165\u6587\u4ef6\u5177\u6709\u4ee5\u4e0b\u6807\u9898\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; headers = ['name', 'date', 'time', 'shares', 'price'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u5047\u8bbe\u9009\u5b9a\u7684\u5217\u5982\u4e0b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; select = ['name', 'shares'] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u4e3a\u4e86\u6267\u884c\u6b63\u786e\u7684\u9009\u62e9\uff0c\u5fc5\u987b\u5c06\u9009\u62e9\u7684\u5217\u540d\u6620\u5c04\u5230\u6587\u4ef6\u4e2d\u7684\u5217\u7d22\u5f15\u3002\u8fd9\u5c31\u662f\u8be5\u6b65\u9aa4\u6b63\u5728\u6267\u884c\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; indices = [headers.index(colname) for colname in select ] &gt;&gt;&gt; indices [0, 3] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u540d\u79f0\uff08&#8221;name&#8221; \uff09\u662f\u7b2c 0 \u5217\uff0c\u80a1\u4efd\u6570\u76ee\uff08&#8221;shares&#8221; \uff09\u662f\u7b2c 3 \u5217\u3002<\/p>\n<p>\u5f53\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u884c\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u7d22\u5f15\u5bf9\u5176\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; row = ['AA', '6\/11\/2007', '9:50am', '100', '32.20' ] &gt;&gt;&gt; row = [ row[index] for index in indices ] &gt;&gt;&gt; row ['AA', '100'] &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u7ec3\u4e60 3.5\uff1a\u6267\u884c\u7c7b\u578b\u8f6c\u6362<\/h3>\n<p>\u4fee\u6539 <code>parse_csv()<\/code> \u51fd\u6570\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u9009\u62e9\u5c06\u7c7b\u578b\u8f6c\u6362\u5e94\u7528\u5230\u8fd4\u56de\u6570\u636e\u4e0a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; portfolio = parse_csv('Data\/portfolio.csv', types=[str, int, float]) &gt;&gt;&gt; portfolio [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}, {'price': 70.44, 'name': 'IBM', 'shares': 100}]  &gt;&gt;&gt; shares_held = parse_csv('Data\/portfolio.csv', select=['name', 'shares'], types=[str, int]) &gt;&gt;&gt; shares_held [{'name': 'AA', 'shares': 100}, {'name': 'IBM', 'shares': 50}, {'name': 'CAT', 'shares': 150}, {'name': 'MSFT', 'shares': 200}, {'name': 'GE', 'shares': 95}, {'name': 'MSFT', 'shares': 50}, {'name': 'IBM', 'shares': 100}] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u5728 \u7ec3\u4e60 2.24 \u4e2d\u5df2\u7ecf\u5bf9\u6b64\u8fdb\u884c\u4e86\u63a2\u7d22\u3002\u9700\u8981\u5c06\u4e0b\u5217\u4ee3\u7801\u7247\u6bb5\u63d2\u5165\u5230\u9898\u89e3\u4e2d\uff1a<\/p>\n<pre><code>... if types:     row = [func(val) for func, val in zip(types, row) ] ... <\/code><\/pre>\n<h3>\u7ec3\u4e60 3.6\uff1a\u5904\u7406\u65e0\u6807\u9898\u7684\u6570\u636e<\/h3>\n<p>\u67d0\u4e9b CSV \u6587\u4ef6\u4e0d\u5305\u542b\u4efb\u4f55\u7684\u6807\u9898\u4fe1\u606f\u3002\u4f8b\u5982\uff0c<code>prices.csv<\/code> \u6587\u4ef6\u770b\u8d77\u6765\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code>\"AA\",9.22 \"AXP\",24.85 \"BA\",44.85 \"BAC\",11.27 ... <\/code><\/pre>\n<p>\u4fee\u6539 <code>parse_csv()<\/code> \u6587\u4ef6\u4ee5\u4fbf\u901a\u8fc7\u521b\u5efa\u5143\u7ec4\u5217\u8868\u6765\u5904\u7406\u6b64\u7c7b\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; prices = parse_csv('Data\/prices.csv', types=[str,float], has_headers=False) &gt;&gt;&gt; prices [('AA', 9.22), ('AXP', 24.85), ('BA', 44.85), ('BAC', 11.27), ('C', 3.72), ('CAT', 35.46), ('CVX', 66.67), ('DD', 28.47), ('DIS', 24.22), ('GE', 13.48), ('GM', 0.75), ('HD', 23.16), ('HPQ', 34.35), ('IBM', 106.28), ('INTC', 15.72), ('JNJ', 55.16), ('JPM', 36.9), ('KFT', 26.11), ('KO', 49.16), ('MCD', 58.99), ('MMM', 57.1), ('MRK', 27.58), ('MSFT', 20.89), ('PFE', 15.19), ('PG', 51.94), ('T', 24.79), ('UTX', 52.61), ('VZ', 29.26), ('WMT', 49.74), ('XOM', 69.35)] &gt;&gt;&gt; <\/code><\/pre>\n<p>\u8981\u6267\u884c\u6b64\u66f4\u6539\uff0c\u9700\u8981\u4fee\u6539\u4ee3\u7801\u4ee5\u4fbf\u6570\u636e\u7684\u7b2c\u4e00\u884c\u4e0d\u88ab\u89e3\u91ca\u4e3a\u6807\u9898\u884c\u3002\u53e6\u5916\uff0c\u9700\u8981\u786e\u4fdd\u4e0d\u521b\u5efa\u5b57\u5178\uff0c\u56e0\u4e3a\u4e0d\u518d\u6709\u53ef\u7528\u4e8e\u5217\u540d\u7684\u952e\u3002<\/p>\n<h3>\u7ec3\u4e60 3.7\uff1a\u9009\u62e9\u5176\u5b83\u7684\u5217\u5206\u9694\u7b26<\/h3>\n<p>\u5c3d\u7ba1 CSV \u6587\u4ef6\u975e\u5e38\u666e\u904d\uff0c\u4f46\u8fd8\u53ef\u80fd\u4f1a\u9047\u5230\u4f7f\u7528\u5176\u5b83\u5217\u5206\u9694\u7b26\uff08\u5982 \u5236\u8868\u7b26\uff08 tab \uff09 \u6216\u7a7a\u683c\u7b26\uff08 space \uff09\uff09\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u4e0b\u6240\u793a\u7684 <code>Data\/portfolio.dat<\/code> \u6587\u4ef6\uff1a<\/p>\n<pre><code>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 <\/code><\/pre>\n<p><code>csv.reader()<\/code> \u51fd\u6570\u5141\u8bb8\u50cf\u4e0b\u9762\u8fd9\u6837\u6307\u5b9a\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff1a<\/p>\n<pre><code>rows = csv.reader(f, delimiter=' ') <\/code><\/pre>\n<p>\u4fee\u6539 <code>parse_csv()<\/code> \u51fd\u6570\u4ee5\u4fbf\u4e5f\u5141\u8bb8\u4fee\u6539\u5206\u9694\u7b26\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; portfolio = parse_csv('Data\/portfolio.dat', types=[str, int, float], delimiter=' ') &gt;&gt;&gt; portfolio [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}] &gt;&gt;&gt; <\/code><\/pre>\n<h3>\u8bf4\u660e<\/h3>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u5b8c\u6210\uff0c\u90a3\u4e48\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5e93\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u53bb\u89e3\u6790\u4efb\u610f\u7684 CSV \u6587\u4ef6\uff0c\u9009\u62e9\u611f\u5174\u8da3\u7684\u5217\uff0c\u6267\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u7528\u5bf9\u6587\u4ef6\u6216\u8005 <code>csv<\/code> \u6a21\u5757\u7684\u5185\u90e8\u5de5\u4f5c\u6709\u592a\u591a\u7684\u62c5\u5fc3\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>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5414952\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hsfzxjy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u300c\u5173\u952e\u5b57\u53c2\u6570\u63d0\u9ad8\u4e86\u4ee3\u7801\u7b80\u6d01\u6027\u300d-&gt;\u300c\u5173\u952e\u5b57\u53c2\u6570\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u300d<\/p>\n<p>clarity \u662f\u4e3a\u660e\u6670\u7684\u610f\u601d                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5414953\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : codists <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hsfzxjy <br \/>\u745e\u601d\u62dc\uff0c\u679c\u7136\u662f\u5927\u4f6c\u3002<br \/>\u4e4b\u524d\u67e5\u4e86\u5b98\u65b9\u6587\u6863\uff0c\u5927\u591a\u628a\u8fd9\u4e2a\u8bcd\u7ffb\u8bd1\u6210\u201c\u6e05\u695a\u201d\uff0c\u4f46\u662f\u611f\u89c9\u7528\u5728\u8fd9\u91cc\u4e0d\u5927\u597d\u3002\u6839\u636e\u5355\u8bcd\u7684\u610f\u601d\uff0c&#8221;the quality of being clear and easy to understand&#8221;\u2014\u2014\u4e0d\u4ec5\u7b80\u6d01\uff0c\u540c\u65f6\u4e5f\u8ba9\u4eba\u66f4\u5bb9\u6613\u7406\u89e3\uff0c\u786e\u5b9e\u7ffb\u8bd1\u6210\u201c\u53ef\u8bfb\u6027\u201d\u66f4\u597d\uff0c\u5df2\u5728 github \u4fee\u6539\u3002<br \/>\u518d\u6b21\u611f\u8c22\u5927\u4f6c\u7684\u6307\u5bfc\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\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\/360932"}],"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=360932"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/360932\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360932"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}