Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Module csv

Parse and write delimited text using dialect rules for separators and quoting.

Version: Python 3.14; repository image, production runtime not attested

Syntax

csv.reader(text_stream, delimiter=",")

Parse and write delimited text using dialect rules for separators and quoting.

The default reader returns fields as strings, preserving leading zeros. Quoting lets one field contain a separator or newline without splitting a record. Open real CSV files with newline="" so the module handles embedded line endings.

The dialect’s lineterminator controls writer output, while the reader recognizes CR or LF independently of this option.

Examples

import csv
import io

buffer = io.StringIO(newline="")
writer = csv.writer(buffer)
writer.writerow(["007", "A,B", "line1\nline2"])
buffer.seek(0)
row = next(csv.reader(buffer))
print(row, len(row))
written = io.StringIO(newline="")
csv.writer(written, lineterminator="|").writerow(["a", "b"])
assert written.getvalue() == "a,b|"
assert list(csv.reader(io.StringIO("a,b|\rc,d\n", newline=""), lineterminator="|")) == [["a", "b|"], ["c", "d"]]

Result

['007', 'A,B', 'line1\nline2'] 3

The dialect’s lineterminator controls writer output, while the reader recognizes CR or LF independently of this option.

Sources