用法:
class csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
創建一個像普通閱讀器一樣操作的對象,但將每行中的信息映射到
dict
,其鍵由可選的fieldnames
參數給出。fieldnames
參數是一個序列。如果省略fieldnames
,則文件f
第一行中的值將用作字段名。不管字段名是如何確定的,字典都會保留它們的原始順序。如果一行的字段多於字段名,則將剩餘數據放入列表中並使用
restkey
指定的字段名(默認為None
)存儲。如果非空白行的字段少於字段名,則缺少的值為 filled-in,值為restval
(默認為None
)。所有其他可選或關鍵字參數都傳遞給底層
reader
實例。在 3.6 版中更改:返回的行現在是類型
OrderedDict
.在 3.8 版中更改:返回的行現在是類型dict.
一個簡短的使用示例:
>>> import csv >>> with open('names.csv', newline='') as csvfile: ... reader = csv.DictReader(csvfile) ... for row in reader: ... print(row['first_name'], row['last_name']) ... Eric Idle John Cleese >>> print(row) {'first_name': 'John', 'last_name': 'Cleese'}
相關用法
- Python csv.DictWriter用法及代碼示例
- Python csv.Dialect用法及代碼示例
- Python csv.reader用法及代碼示例
- Python csv.writer用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python cudf.Series.ceil用法及代碼示例
- Python cudf.core.column.string.StringMethods.endswith用法及代碼示例
- Python cuxfilter.charts.datashader.heatmap用法及代碼示例
- Python cudf.Series.update用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python cusignal.windows.windows.hann用法及代碼示例
- Python cudf.DataFrame.mod用法及代碼示例
- Python cudf.DataFrame.isin用法及代碼示例
- Python cudf.core.column.string.StringMethods.title用法及代碼示例
- Python cuml.metrics.pairwise_distances.pairwise_distances用法及代碼示例
- Python collections.somenamedtuple._replace用法及代碼示例
- Python cuxfilter.charts.panel_widgets.int_slider用法及代碼示例
- Python cudf.DataFrame.rmul用法及代碼示例
- Python cudf.Series.max用法及代碼示例
- Python cmath.isclose()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 csv.DictReader。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。