Lines Matching full:csv
1 :mod:`!csv` --- CSV File Reading and Writing
4 .. module:: csv
9 **Source code:** :source:`Lib/csv.py`
12 single: csv
17 The so-called CSV (Comma Separated Values) format is the most common import and
18 export format for spreadsheets and databases. CSV format was used for many
22 differences can make it annoying to process CSV files from multiple sources.
28 The :mod:`csv` module implements classes to read and write tabular data in CSV
31 knowing the precise details of the CSV format used by Excel. Programmers can
32 also describe the CSV formats understood by other applications or define their
33 own special-purpose CSV formats.
35 The :mod:`csv` module's :class:`reader` and :class:`writer` objects read and
41 :pep:`305` - CSV File API
50 The :mod:`csv` module defines the following functions:
54 single: universal newlines; csv.reader function
60 strings, each in the reader's defined csv format.
65 specific to a particular CSV dialect. It may be an instance of a subclass of
70 section :ref:`csv-fmt-params`.
72 Each row read from the csv file is returned as a list of strings. No
78 >>> import csv
79 >>> with open('eggs.csv', newline='') as csvfile:
80 ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
94 particular CSV dialect. It may be an instance of a subclass of the
99 the :ref:`csv-fmt-params` section. To make it
103 CSV files without preprocessing the data returned from a ``cursor.fetch*`` call.
108 import csv
109 with open('eggs.csv', 'w', newline='') as csvfile:
110 spamwriter = csv.writer(csvfile, delimiter=' ',
111 quotechar='|', quoting=csv.QUOTE_MINIMAL)
122 parameters, see section :ref:`csv-fmt-params`.
148 The :mod:`csv` module defines the following classes:
183 >>> import csv
184 >>> with open('names.csv', newline='') as csvfile:
185 ... reader = csv.DictReader(csvfile)
221 import csv
223 with open('names.csv', 'w', newline='') as csvfile:
225 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
237 Due to the lack of a strict CSV specification, different applications
238 produce subtly different CSV data. :class:`Dialect` instances define how
245 import csv
247 with open('students.csv', 'w', newline='') as csvfile:
248 writer = csv.writer(csvfile, dialect='unix')
253 The :class:`excel` class defines the usual properties of an Excel-generated CSV
265 The :class:`unix_dialect` class defines the usual properties of a CSV file
274 The :class:`Sniffer` class is used to deduce the format of a CSV file.
288 Analyze the sample text (presumed to be in CSV format) and return
307 with open('example.csv', newline='') as csvfile:
308 dialect = csv.Sniffer().sniff(csvfile.read(1024))
310 reader = csv.reader(csvfile, dialect)
311 # ... process CSV file contents here ...
316 The :mod:`csv` module defines the following constants:
368 The :mod:`csv` module defines the following exception:
383 describing the format of the CSV file. When creating :class:`reader` or
443 reader. It can take on any of the :ref:`QUOTE_\* constants <csv-constants>`
455 When ``True``, raise exception :exc:`Error` on bad CSV input.
506 read CSV files (assuming they support complex numbers at all).
552 The simplest example of reading a CSV file::
554 import csv
555 with open('some.csv', newline='') as f:
556 reader = csv.reader(f)
562 import csv
564 reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
570 import csv
571 with open('some.csv', 'w', newline='') as f:
572 writer = csv.writer(f)
575 Since :func:`open` is used to open a CSV file for reading, the file
580 import csv
581 with open('some.csv', newline='', encoding='utf-8') as f:
582 reader = csv.reader(f)
591 import csv
592 csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
594 reader = csv.reader(f, 'unixpwd')
598 import csv, sys
599 filename = 'some.csv'
601 reader = csv.reader(f)
605 except csv.Error as e:
611 import csv
612 for row in csv.reader(['one,two,three']):
621 ``newline=''``, since the csv module does its own