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
64 specific to a particular CSV dialect. It may be an instance of a subclass of
69 section :ref:`csv-fmt-params`.
71 Each row read from the csv file is returned as a list of strings. No
77 >>> import csv
78 >>> with open('eggs.csv', newline='') as csvfile:
79 ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
93 particular CSV dialect. It may be an instance of a subclass of the
98 the :ref:`csv-fmt-params` section. To make it
102 CSV files without preprocessing the data returned from a ``cursor.fetch*`` call.
107 import csv
108 with open('eggs.csv', 'w', newline='') as csvfile:
109 spamwriter = csv.writer(csvfile, delimiter=' ',
110 quotechar='|', quoting=csv.QUOTE_MINIMAL)
121 parameters, see section :ref:`csv-fmt-params`.
147 The :mod:`csv` module defines the following classes:
178 >>> import csv
179 >>> with open('names.csv', newline='') as csvfile:
180 ... reader = csv.DictReader(csvfile)
214 import csv
216 with open('names.csv', 'w', newline='') as csvfile:
218 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
230 Due to the lack of a strict CSV specification, different applications
231 produce subtly different CSV data. :class:`Dialect` instances define how
238 import csv
240 with open('students.csv', 'w', newline='') as csvfile:
241 writer = csv.writer(csvfile, dialect='unix')
247 The :class:`excel` class defines the usual properties of an Excel-generated CSV
259 The :class:`unix_dialect` class defines the usual properties of a CSV file
268 The :class:`Sniffer` class is used to deduce the format of a CSV file.
282 Analyze the sample text (presumed to be in CSV format) and return
301 with open('example.csv', newline='') as csvfile:
302 dialect = csv.Sniffer().sniff(csvfile.read(1024))
304 reader = csv.reader(csvfile, dialect)
305 # ... process CSV file contents here ...
308 The :mod:`csv` module defines the following constants:
338 The :mod:`csv` module defines the following exception:
414 :ref:`csv-contents`) and defaults to :const:`QUOTE_MINIMAL`.
425 When ``True``, raise exception :exc:`Error` on bad CSV input.
474 read CSV files (assuming they support complex numbers at all).
520 The simplest example of reading a CSV file::
522 import csv
523 with open('some.csv', newline='') as f:
524 reader = csv.reader(f)
530 import csv
532 reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
538 import csv
539 with open('some.csv', 'w', newline='') as f:
540 writer = csv.writer(f)
543 Since :func:`open` is used to open a CSV file for reading, the file
548 import csv
549 with open('some.csv', newline='', encoding='utf-8') as f:
550 reader = csv.reader(f)
559 import csv
560 csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
562 reader = csv.reader(f, 'unixpwd')
566 import csv, sys
567 filename = 'some.csv'
569 reader = csv.reader(f)
573 except csv.Error as e:
579 import csv
580 for row in csv.reader(['one,two,three']):
589 ``newline=''``, since the csv module does its own