Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2023-03-02 16:07:59
Size: 2191
Comment:
Revision 5 as of 2023-10-13 14:14:53
Size: 2084
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

'''`csv`''' is a module that supports (de-)serializing CSV files.
Line 38: Line 40:
=== Quote_All ===

Passing the constant `csv.QUOTE_ALL` as the `quoting` option on a CSV writer instructs the formatter to quote all fields using the `quotechar` option's character.

----



=== Quote_Minimal ===


Passing the constant `csv.QUOTE_MINIMAL` as the `quoting` option on a CSV writer instructs the formatter to quote fields which contain ''special characters'' using the `quotechar` option's character.

''Special characters'' include all characters set in the `delimiter`, `quotechar`, and `lineterminator` options.

----



=== Quote_NonNumeric ===

Passing the constant `csv.QUOTE_NONNUMERIC` as the `quoting` option on a CSV writer instructs the formatter to quote all non-numeric fields using the `quotechar` option's character.

Passing as the `quoting` option on a CSV reader instructs the parser to convert all fields not quoted by the `quotechar` option's character into `float`s.

----



=== Quote_None ===

Passing the constant `csv.QUOTE_NONE` as the `quoting` option on a CSV writer instructs the formatter to never quote fields. If the `delimiter` options's character is encountered in output, it is escaped with the `escapechar` options's character. If the `escapechar` option is not set when it is required, `csv.Error` is raised.

Passing as the `quoting` option on a CSV reader instructs the parser to retain the `quotechar` option's character in data.

----


Line 100: Line 63:
== Constants ==

The above functions take one of the following constants as a '''`quoting`''' option. The ensuing behavior often depends on other options passed on those functions, such as '''`quotechar`'''.

||'''Constant''' ||'''Meaning''' ||
||`csv.QUOTE_ALL` ||quote all fields using the `quotechar` ||
||`csv.QUOTE_MINIMAL` ||quote fields which contain special characters (`delimiter`, `quotechar`, and `lineterminator`) using the `quotechar` ||
||`csv.QUOTE_NONNUMERIC`||quote all non-numeric fields using the `quotechar` ||
||`csv.QUOTE_NONE` ||never quote fields; if `delimiter` is encountered in output, it is escaped with `escapechar` or `csv.Error` is raised||

If `csv.QUOTE_NONNUMERIC` is passed to a reader, all unquoted fields are cast to [[Python/Builtins/Types#Float|float]].

If `csv.QUOTE_NONE` is passed to a reader, all quotes are retained as part of a field's data.

----


Line 103: Line 84:

[[https://pymotw.com/3/csv/|Python Module of the Day article for csv]]

Python Csv

csv is a module that supports (de-)serializing CSV files.


Usage

DictReader


DictWriter


Reader

import csv

with open(file, 'r', newline='', encoding='latin-1') as f:
    r = csv.reader(f)
    for row in r:
        print(list(row))


Writer

import csv

data = [["John", "Doe", 123], ["Jane", "Doe", 456]]

with open(file, 'w', newline='', encoding='latin-1') as f:
    w = csv.writer(f)
    w.writerows(data)

Alternatively, write individual rows.

for row in data:
    w.writerow(data)


Constants

The above functions take one of the following constants as a quoting option. The ensuing behavior often depends on other options passed on those functions, such as quotechar.

Constant

Meaning

csv.QUOTE_ALL

quote all fields using the quotechar

csv.QUOTE_MINIMAL

quote fields which contain special characters (delimiter, quotechar, and lineterminator) using the quotechar

csv.QUOTE_NONNUMERIC

quote all non-numeric fields using the quotechar

csv.QUOTE_NONE

never quote fields; if delimiter is encountered in output, it is escaped with escapechar or csv.Error is raised

If csv.QUOTE_NONNUMERIC is passed to a reader, all unquoted fields are cast to float.

If csv.QUOTE_NONE is passed to a reader, all quotes are retained as part of a field's data.


See also

Python csv module documentation

Python Module of the Day article for csv


CategoryRicottone

Python/Csv (last edited 2023-10-13 14:14:53 by DominicRicottone)