org.pycforge.csv · Author: PyCForge
License: GPL-3.0-only · Created: 2026-11-13
Runtime conversion · Direct declared calls
Using this import · Functions · Conversion contract · Worked examples
DictReader · DictWriter · get_dialect · list_dialects · reader · register_dialect · unregister_dialect · writer
Dictionary and sequence records, six quoting modes, escaping, dialect snapshots and qualified Error handling.
Use the documented names under your selected namespace. Open UTF-8 text files with newline=""; keep them open for the reader/writer lifetime.
Runtime only. DictReader uses the first record as headers when fieldnames=None; supplied list/tuple headers are retained and iterator headers are materialized. Rows are fresh ordered dictionaries; duplicate headers keep the last value. Blank data records are skipped. Extra columns become a list under restkey (default None); missing columns use the retained restval (default None). fieldnames access loads headers lazily. DictWriter uses explicit field order, restval="", and extrasaction="raise" or "ignore" (case-insensitive). writerow/writeheader return Unicode character counts; writerows returns None. DictWriter methods accept rowdict/rowdicts keywords; sequence-writer methods are positional-only. Writer rows require Runtime dictionaries; sequence writers accept admitted iterables of str, int64, finite float64, bool or None. Caller-owned Runtime text files stay open; use newline="" and encoding="utf-8". Rows are formatted before writing; earlier rows and device partial writes are not rolled back. Aliases share state. QUOTE_MINIMAL=0, ALL=1, NONNUMERIC=2, NONE=3, STRINGS=4 and NOTNULL=5 are supported; the latter two use Python 3.13+ reader behavior, not the Python 3.12 reader bug. Unquoted nonempty numeric fields convert to float64 in NONNUMERIC/STRINGS; invalid numbers raise ValueError; nonfinite/overflowing conversions remain outside finite float64 and raise ValueError/OverflowError. Empty unquoted fields become None in STRINGS/NOTNULL. A single None field cannot form a distinguishable unquoted record and raises Error in STRINGS/NOTNULL, following Python 3.13.5. Delimiter, quotechar and escapechar accept one Unicode scalar (quote/escape may be None); doublequote, skipinitialspace and strict use truth values. Reader recognizes CR/LF regardless of lineterminator. Character options must differ and cannot be CR/LF; quote/escape cannot be space with skipinitialspace=True (Python 3.13 validation). Dialects excel, excel-tab and unix are initially registered. Omitted/None reader/writer/registration dialect uses fixed Excel settings; DictReader/DictWriter default to the named excel registration. register/get/unregister/list_dialects share a registry within one generated Runtime context, across all profile-0.2 namespaces and supplied modules; no process-global or persistent registry. Registration accepts a registered name or immutable get_dialect snapshot plus keyword overrides. Existing readers/writers retain their format snapshot after replacement/unregistration. Dialect subclasses, arbitrary attribute objects and custom write callbacks are unsupported. Reader/dictionary/writer attributes are read-only; fieldname list contents may be mutated. Error is a qualified catch/raise token, not a first-class exception object; except Error as e and bare re-raise remain outside the Runtime exception profile. Malformed CSV, invalid source items, unknown dialects and format limits raise Error, separately from TypeError/ValueError for arguments, numeric conversion and extra dictionary keys. I/O errors retain OSError or the existing closed-file ValueError. Allocation/work/depth failures remain fatal, never Error. Limits: 131072 Unicode scalars per field, 16384 fields per row, 1 MiB UTF-8 per input record or formatted row; 128 registered dialects, 1024-byte names/terminators. Sniffer, field_size_limit, binary input, arbitrary objects and complete Python csv compatibility are not provided. Helpers never collect internally; retained values are precisely traced. Collection is deferred while files are open. Legacy profile 0.1 keeps its original behavior.
Adds versioned dictionary, format and error contracts; retains the legacy profile.
Use *import csv in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.
Runtime only. DictReader uses the first record as headers when fieldnames=None; supplied list/tuple headers are retained and iterator headers are materialized. Rows are fresh ordered dictionaries; duplicate headers keep the last value. Blank data records are skipped. Extra columns become a list under restkey (default None); missing columns use the retained restval (default None). fieldnames access loads headers lazily. DictWriter uses explicit field order, restval="", and extrasaction="raise" or "ignore" (case-insensitive). writerow/writeheader return Unicode character counts; writerows returns None. DictWriter methods accept rowdict/rowdicts keywords; sequence-writer methods are positional-only. Writer rows require Runtime dictionaries; sequence writers accept admitted iterables of str, int64, finite float64, bool or None. Caller-owned Runtime text files stay open; use newline="" and encoding="utf-8". Rows are formatted before writing; earlier rows and device partial writes are not rolled back. Aliases share state. QUOTE_MINIMAL=0, ALL=1, NONNUMERIC=2, NONE=3, STRINGS=4 and NOTNULL=5 are supported; the latter two use Python 3.13+ reader behavior, not the Python 3.12 reader bug. Unquoted nonempty numeric fields convert to float64 in NONNUMERIC/STRINGS; invalid numbers raise ValueError; nonfinite/overflowing conversions remain outside finite float64 and raise ValueError/OverflowError. Empty unquoted fields become None in STRINGS/NOTNULL. A single None field cannot form a distinguishable unquoted record and raises Error in STRINGS/NOTNULL, following Python 3.13.5. Delimiter, quotechar and escapechar accept one Unicode scalar (quote/escape may be None); doublequote, skipinitialspace and strict use truth values. Reader recognizes CR/LF regardless of lineterminator. Character options must differ and cannot be CR/LF; quote/escape cannot be space with skipinitialspace=True (Python 3.13 validation). Dialects excel, excel-tab and unix are initially registered. Omitted/None reader/writer/registration dialect uses fixed Excel settings; DictReader/DictWriter default to the named excel registration. register/get/unregister/list_dialects share a registry within one generated Runtime context, across all profile-0.2 namespaces and supplied modules; no process-global or persistent registry. Registration accepts a registered name or immutable get_dialect snapshot plus keyword overrides. Existing readers/writers retain their format snapshot after replacement/unregistration. Dialect subclasses, arbitrary attribute objects and custom write callbacks are unsupported. Reader/dictionary/writer attributes are read-only; fieldname list contents may be mutated. Error is a qualified catch/raise token, not a first-class exception object; except Error as e and bare re-raise remain outside the Runtime exception profile. Malformed CSV, invalid source items, unknown dialects and format limits raise Error, separately from TypeError/ValueError for arguments, numeric conversion and extra dictionary keys. I/O errors retain OSError or the existing closed-file ValueError. Allocation/work/depth failures remain fatal, never Error. Limits: 131072 Unicode scalars per field, 16384 fields per row, 1 MiB UTF-8 per input record or formatted row; 128 registered dialects, 1024-byte names/terminators. Sniffer, field_size_limit, binary input, arbitrary objects and complete Python csv compatibility are not provided. Helpers never collect internally; retained values are precisely traced. Collection is deferred while files are open. Legacy profile 0.1 keeps its original behavior.
csv.QUOTE_MINIMAL: 0
csv.QUOTE_ALL: 1
csv.QUOTE_NONNUMERIC: 2
csv.QUOTE_NONE: 3
csv.QUOTE_STRINGS: 4
csv.QUOTE_NOTNULL: 5
csv.Error: csv.Error
Read ordered dictionary rows with lazy headers and restkey/restval.
f: positional-or-keyword; required
fieldnames: positional-or-keyword; default None
restkey: positional-or-keyword; default None
restval: positional-or-keyword; default None
dialect: positional-or-keyword; default 'excel'
delimiter: keyword-only; default omitted
quotechar: keyword-only; default omitted
escapechar: keyword-only; default omitted
doublequote: keyword-only; default omitted
skipinitialspace: keyword-only; default omitted
lineterminator: keyword-only; default omitted
quoting: keyword-only; default omitted
strict: keyword-only; default omitted
Argument 1: f: positional-or-keyword (required)
Argument 2: fieldnames: positional-or-keyword (default None)
Argument 3: restkey: positional-or-keyword (default None)
Argument 4: restval: positional-or-keyword (default None)
Argument 5: dialect: positional-or-keyword (default 'excel')
Argument 6: delimiter: keyword-only (default omitted)
Argument 7: quotechar: keyword-only (default omitted)
Argument 8: escapechar: keyword-only (default omitted)
Argument 9: doublequote: keyword-only (default omitted)
Argument 10: skipinitialspace: keyword-only (default omitted)
Argument 11: lineterminator: keyword-only (default omitted)
Argument 12: quoting: keyword-only (default omitted)
Argument 13: strict: keyword-only (default omitted)
Returns: record-reader
View example: dictionary-roundtrip
Write dictionary rows in explicit column order; expose writeheader().
f: positional-or-keyword; required
fieldnames: positional-or-keyword; required
restval: positional-or-keyword; default ''
extrasaction: positional-or-keyword; default 'raise'
dialect: positional-or-keyword; default 'excel'
delimiter: keyword-only; default omitted
quotechar: keyword-only; default omitted
escapechar: keyword-only; default omitted
doublequote: keyword-only; default omitted
skipinitialspace: keyword-only; default omitted
lineterminator: keyword-only; default omitted
quoting: keyword-only; default omitted
strict: keyword-only; default omitted
Argument 1: f: positional-or-keyword (required)
Argument 2: fieldnames: positional-or-keyword (required)
Argument 3: restval: positional-or-keyword (default '')
Argument 4: extrasaction: positional-or-keyword (default 'raise')
Argument 5: dialect: positional-or-keyword (default 'excel')
Argument 6: delimiter: keyword-only (default omitted)
Argument 7: quotechar: keyword-only (default omitted)
Argument 8: escapechar: keyword-only (default omitted)
Argument 9: doublequote: keyword-only (default omitted)
Argument 10: skipinitialspace: keyword-only (default omitted)
Argument 11: lineterminator: keyword-only (default omitted)
Argument 12: quoting: keyword-only (default omitted)
Argument 13: strict: keyword-only (default omitted)
Returns: record-writer
View example: dictionary-roundtrip
Look up an immutable format snapshot in this Runtime context.
name: positional-only; required
Argument 1: name: positional-only (required)
Returns: record-dialect
View example: dictionary-roundtrip
Return a fresh list of registered names in insertion order.
Argument labels indicate positions; they are not accepted keyword names.
Returns: record-value
View example: dictionary-roundtrip
Parse rows with a reusable dialect, quoting and escaping.
iterable: positional-only; required
dialect: positional-or-keyword; default None
delimiter: keyword-only; default omitted
quotechar: keyword-only; default omitted
escapechar: keyword-only; default omitted
doublequote: keyword-only; default omitted
skipinitialspace: keyword-only; default omitted
lineterminator: keyword-only; default omitted
quoting: keyword-only; default omitted
strict: keyword-only; default omitted
Argument 1: iterable: positional-only (required)
Argument 2: dialect: positional-or-keyword (default None)
Argument 3: delimiter: keyword-only (default omitted)
Argument 4: quotechar: keyword-only (default omitted)
Argument 5: escapechar: keyword-only (default omitted)
Argument 6: doublequote: keyword-only (default omitted)
Argument 7: skipinitialspace: keyword-only (default omitted)
Argument 8: lineterminator: keyword-only (default omitted)
Argument 9: quoting: keyword-only (default omitted)
Argument 10: strict: keyword-only (default omitted)
Returns: record-reader
View example: dictionary-roundtrip
Register or replace an immutable format snapshot in this Runtime context.
name: positional-only; required
dialect: positional-or-keyword; default None
delimiter: keyword-only; default omitted
quotechar: keyword-only; default omitted
escapechar: keyword-only; default omitted
doublequote: keyword-only; default omitted
skipinitialspace: keyword-only; default omitted
lineterminator: keyword-only; default omitted
quoting: keyword-only; default omitted
strict: keyword-only; default omitted
Argument 1: name: positional-only (required)
Argument 2: dialect: positional-or-keyword (default None)
Argument 3: delimiter: keyword-only (default omitted)
Argument 4: quotechar: keyword-only (default omitted)
Argument 5: escapechar: keyword-only (default omitted)
Argument 6: doublequote: keyword-only (default omitted)
Argument 7: skipinitialspace: keyword-only (default omitted)
Argument 8: lineterminator: keyword-only (default omitted)
Argument 9: quoting: keyword-only (default omitted)
Argument 10: strict: keyword-only (default omitted)
Returns: record-value
View example: dictionary-roundtrip
Remove a registered name without changing existing users.
name: positional-only; required
Argument 1: name: positional-only (required)
Returns: record-value
View example: dictionary-roundtrip
Write sequence rows using a reusable dialect, quoting and escaping.
file: positional-only; required
dialect: positional-or-keyword; default None
delimiter: keyword-only; default omitted
quotechar: keyword-only; default omitted
escapechar: keyword-only; default omitted
doublequote: keyword-only; default omitted
skipinitialspace: keyword-only; default omitted
lineterminator: keyword-only; default omitted
quoting: keyword-only; default omitted
strict: keyword-only; default omitted
Argument 1: file: positional-only (required)
Argument 2: dialect: positional-or-keyword (default None)
Argument 3: delimiter: keyword-only (default omitted)
Argument 4: quotechar: keyword-only (default omitted)
Argument 5: escapechar: keyword-only (default omitted)
Argument 6: doublequote: keyword-only (default omitted)
Argument 7: skipinitialspace: keyword-only (default omitted)
Argument 8: lineterminator: keyword-only (default omitted)
Argument 9: quoting: keyword-only (default omitted)
Argument 10: strict: keyword-only (default omitted)
Returns: record-writer
View example: dictionary-roundtrip
Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.
*import csv as records
def main():
records.register_dialect("report", "excel", delimiter=";", lineterminator="\n")
fmt = records.get_dialect("report")
print(records.list_dialects())
rows = records.DictReader(['name;value\n', 'north;12\n', 'south\n'], dialect=fmt, restval="0")
with open("records.csv", "w+", encoding="utf-8", newline="") as file:
writer = records.DictWriter(file, ["value", "name"], dialect=fmt)
print(writer.writeheader(), writer.writerows(rows))
print(writer.writerow({"name": "west", "value": 8}))
file.seek(0)
print(list(records.reader(file, fmt)))
file.seek(0)
print(records.writer(file, fmt).writerow(["done", None]))
records.unregister_dialect("report")
try:
records.get_dialect("report")
except records.Error:
print("unknown dialect")
return 0
Expected standard output:
['excel', 'excel-tab', 'unix', 'report'] 11 None 7 [['value', 'name'], ['12', 'north'], ['0', 'south'], ['8', 'west']] 6 unknown dialect
Expected exit code: 0