用法:
class csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
創建一個像普通編寫器一樣操作但將字典映射到輸出行的對象。
fieldnames
參數是一個sequence
鍵,用於標識傳遞給writerow()
方法的字典中的值寫入文件f
的順序。如果字典缺少fieldnames
中的鍵,可選的restval
參數指定要寫入的值。如果傳遞給writerow()
方法的字典包含在fieldnames
中找不到的鍵,則可選的extrasaction
參數指示要采取的操作。如果它設置為'raise'
(默認值),則會引發ValueError
。如果它設置為'ignore'
,字典中的額外值將被忽略。任何其他可選或關鍵字參數都將傳遞給基礎writer
實例。請注意,與
DictReader
類不同,DictWriter
類的fieldnames
參數不是可選的。一個簡短的使用示例:
import csv with open('names.csv', 'w', newline='') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'}) writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'}) writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
相關用法
- Python csv.DictReader用法及代碼示例
- 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.DictWriter。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。