Differences between revisions 1 and 2
Revision 1 as of 2023-03-01 17:34:43
Size: 2160
Comment:
Revision 2 as of 2023-03-02 16:07:59
Size: 2191
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
== DictReader == == Usage ==

=
== DictReader ===
Line 15: Line 17:
== DictWriter == === DictWriter ===
Line 21: Line 23:
== Reader == === Reader ===
Line 36: Line 38:
== Quote_All == === Quote_All ===
Line 44: Line 46:
== Quote_Minimal == === Quote_Minimal ===
Line 55: Line 57:
== Quote_NonNumeric == === Quote_NonNumeric ===
Line 65: Line 67:
== Quote_None == === Quote_None ===
Line 75: Line 77:
== Writer == === Writer ===

Python Csv


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))


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 floats.


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.


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)


See also

Python csv module documentation


CategoryRicottone

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