Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8231de0

Browse files
committed
Many updates to csv module doco.
1 parent 5cfd837 commit 8231de0

1 file changed

Lines changed: 110 additions & 32 deletions

File tree

Doc/lib/libcsv.tex

Lines changed: 110 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ \subsection{Module Contents \label{csv-contents}}
5050
The \module{csv} module defines the following functions:
5151

5252
\begin{funcdesc}{reader}{csvfile\optional{,
53-
dialect=\code{'excel'}\optional{, fmtparam}}}
53+
dialect=\code{'excel'}}\optional{, fmtparam}}
5454
Return a reader object which will iterate over lines in the given
5555
{}\var{csvfile}. \var{csvfile} can be any object which supports the
5656
iterator protocol and returns a string each time its \method{next}
57-
method is called. If \var{csvfile} is a file object, it must be opened with
57+
method is called - file objects and list objects are both suitable.
58+
If \var{csvfile} is a file object, it must be opened with
5859
the 'b' flag on platforms where that makes a difference. An optional
5960
{}\var{dialect} parameter can be given
6061
which is used to define a set of parameters specific to a particular CSV
@@ -71,7 +72,7 @@ \subsection{Module Contents \label{csv-contents}}
7172
\end{funcdesc}
7273

7374
\begin{funcdesc}{writer}{csvfile\optional{,
74-
dialect=\code{'excel'}\optional{, fmtparam}}}
75+
dialect=\code{'excel'}}\optional{, fmtparam}}
7576
Return a writer object responsible for converting the user's data into
7677
delimited strings on the given file-like object. \var{csvfile} can be any
7778
object with a \function{write} method. If \var{csvfile} is a file object,
@@ -94,9 +95,14 @@ \subsection{Module Contents \label{csv-contents}}
9495
with \function{str()} before being written.
9596
\end{funcdesc}
9697

97-
\begin{funcdesc}{register_dialect}{name, dialect}
98-
Associate \var{dialect} with \var{name}. \var{dialect} must be a subclass
99-
of \class{csv.Dialect}. \var{name} must be a string or Unicode object.
98+
\begin{funcdesc}{register_dialect}{name\optional{, dialect}\optional{, fmtparam}}
99+
Associate \var{dialect} with \var{name}. \var{name} must be a string
100+
or Unicode object. The dialect can be specified either by passing a
101+
sub-class of \class{Dialect}, or by \var{fmtparam} keyword arguments,
102+
or both, with keyword arguments overriding parameters of the dialect.
103+
For more information about the dialect and formatting parameters, see
104+
section~\ref{csv-fmt-params}, ``Dialects and Formatting Parameters''
105+
for details of these parameters.
100106
\end{funcdesc}
101107

102108
\begin{funcdesc}{unregister_dialect}{name}
@@ -114,6 +120,12 @@ \subsection{Module Contents \label{csv-contents}}
114120
Return the names of all registered dialects.
115121
\end{funcdesc}
116122

123+
\begin{funcdesc}{field_size_limit}{\optional{new_limit}}
124+
Returns the current maximum field size allowed by the parser. If
125+
\var{new_limit} is given, this becomes the new limit.
126+
\versionadded{2.5}
127+
\end{funcdesc}
128+
117129

118130
The \module{csv} module defines the following classes:
119131

@@ -208,19 +220,25 @@ \subsection{Module Contents \label{csv-contents}}
208220

209221
\begin{datadesc}{QUOTE_MINIMAL}
210222
Instructs \class{writer} objects to only quote those fields which contain
211-
the current \var{delimiter} or begin with the current \var{quotechar}.
223+
special characters such as \var{delimiter}, \var{quotechar} or any of the
224+
characters in \var{lineterminator}.
212225
\end{datadesc}
213226

214227
\begin{datadesc}{QUOTE_NONNUMERIC}
215-
Instructs \class{writer} objects to quote all non-numeric fields.
228+
Instructs \class{writer} objects to quote all non-numeric
229+
fields.
230+
231+
Instructs the reader to convert all non-quoted fields to type \var{float}.
216232
\end{datadesc}
217233

218234
\begin{datadesc}{QUOTE_NONE}
219235
Instructs \class{writer} objects to never quote fields. When the current
220236
\var{delimiter} occurs in output data it is preceded by the current
221-
\var{escapechar} character. When \constant{QUOTE_NONE} is in effect, it
222-
is an error not to have a single-character \var{escapechar} defined, even if
223-
no data to be written contains the \var{delimiter} character.
237+
\var{escapechar} character. If \var{escapechar} is not set, the writer
238+
will raise \exception{Error} if any characters that require escaping
239+
are encountered.
240+
241+
Instructs \class{reader} to perform no special processing of quote characters.
224242
\end{datadesc}
225243

226244

@@ -250,32 +268,43 @@ \subsection{Dialects and Formatting Parameters\label{csv-fmt-params}}
250268
\end{memberdesc}
251269

252270
\begin{memberdesc}[Dialect]{doublequote}
253-
Controls how instances of \var{quotechar} appearing inside a field should be
254-
themselves be quoted. When \constant{True}, the character is doubled.
255-
When \constant{False}, the \var{escapechar} must be a one-character string
256-
which is used as a prefix to the \var{quotechar}. It defaults to
257-
\constant{True}.
271+
Controls how instances of \var{quotechar} appearing inside a field should
272+
be themselves be quoted. When \constant{True}, the character is doubled.
273+
When \constant{False}, the \var{escapechar} is used as a prefix to the
274+
\var{quotechar}. It defaults to \constant{True}.
275+
276+
On output, if \var{doublequote} is \constant{False} and no
277+
\var{escapechar} is set, \exception{Error} is raised if a \var{quotechar}
278+
is found in a field.
258279
\end{memberdesc}
259280

260281
\begin{memberdesc}[Dialect]{escapechar}
261-
A one-character string used to escape the \var{delimiter} if \var{quoting}
262-
is set to \constant{QUOTE_NONE}. It defaults to \constant{None}.
282+
A one-character string used by the writer to escape the \var{delimiter} if
283+
\var{quoting} is set to \constant{QUOTE_NONE} and the \var{quotechar}
284+
if \var{doublequote} is \constant{False}. On reading, the \var{escapechar}
285+
removes any special meaning from the following character. It defaults
286+
to \constant{None}, which disables escaping.
263287
\end{memberdesc}
264288

265289
\begin{memberdesc}[Dialect]{lineterminator}
266-
The string used to terminate lines in the CSV file. It defaults to
267-
\code{'\e r\e n'}.
290+
The string used to terminate lines produced by the \class{writer}.
291+
It defaults to \code{'\e r\e n'}.
292+
293+
\note{The \class{reader} is hard-coded to recognise either \code{'\e r'}
294+
or \code{'\e n'} as end-of-line, and ignores \var{lineterminator}. This
295+
behavior may change in the future.}
268296
\end{memberdesc}
269297

270298
\begin{memberdesc}[Dialect]{quotechar}
271-
A one-character string used to quote elements containing the \var{delimiter}
272-
or which start with the \var{quotechar}. It defaults to \code{'"'}.
299+
A one-character string used to quote fields containing special characters,
300+
such as the \var{delimiter} or \var{quotechar}, or which contain new-line
301+
characters. It defaults to \code{'"'}.
273302
\end{memberdesc}
274303

275304
\begin{memberdesc}[Dialect]{quoting}
276-
Controls when quotes should be generated by the writer. It can take on any
277-
of the \constant{QUOTE_*} constants (see section~\ref{csv-contents})
278-
and defaults to \constant{QUOTE_MINIMAL}.
305+
Controls when quotes should be generated by the writer and recognised
306+
by the reader. It can take on any of the \constant{QUOTE_*} constants
307+
(see section~\ref{csv-contents}) and defaults to \constant{QUOTE_MINIMAL}.
279308
\end{memberdesc}
280309

281310
\begin{memberdesc}[Dialect]{skipinitialspace}
@@ -294,6 +323,17 @@ \subsection{Reader Objects}
294323
according to the current dialect.
295324
\end{methoddesc}
296325

326+
Reader objects have the following public attributes:
327+
328+
\begin{memberdesc}[csv reader]{dialect}
329+
A read-only description of the dialect in use by the parser.
330+
\end{memberdesc}
331+
332+
\begin{memberdesc}[csv reader]{line_num}
333+
The number of lines read from the source iterator. This is not the same
334+
as the number of records returned, as records can span multiple lines.
335+
\end{memberdesc}
336+
297337

298338
\subsection{Writer Objects}
299339

@@ -317,10 +357,17 @@ \subsection{Writer Objects}
317357
according to the current dialect.
318358
\end{methoddesc}
319359

360+
Writer objects have the following public attribute:
361+
362+
\begin{memberdesc}[csv writer]{dialect}
363+
A read-only description of the dialect in use by the writer.
364+
\end{memberdesc}
365+
366+
320367

321368
\subsection{Examples}
322369

323-
The ``Hello, world'' of csv reading is
370+
The simplest example of reading a CSV file:
324371

325372
\begin{verbatim}
326373
import csv
@@ -329,20 +376,51 @@ \subsection{Examples}
329376
print row
330377
\end{verbatim}
331378

332-
To print just the first and last columns of each row try
379+
Reading a file with an alternate format:
333380

334381
\begin{verbatim}
335382
import csv
336-
reader = csv.reader(open("some.csv", "rb"))
383+
reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
337384
for row in reader:
338-
print row[0], row[-1]
385+
print row
339386
\end{verbatim}
340387

341-
The corresponding simplest possible writing example is
388+
The corresponding simplest possible writing example is:
342389

343390
\begin{verbatim}
344391
import csv
345392
writer = csv.writer(open("some.csv", "wb"))
346-
for row in someiterable:
347-
writer.writerow(row)
393+
writer.writerows(someiterable)
348394
\end{verbatim}
395+
396+
Registering a new dialect:
397+
398+
\begin{verbatim}
399+
import csv
400+
401+
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
402+
403+
reader = csv.reader(open("passwd", "rb"), 'unixpwd')
404+
\end{verbatim}
405+
406+
A slightly more advanced use of the reader - catching and reporting errors:
407+
408+
\begin{verbatim}
409+
import csv, sys
410+
filename = "some.csv"
411+
reader = csv.reader(open(filename, "rb"))
412+
try:
413+
for row in reader:
414+
print row
415+
except csv.Error, e:
416+
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
417+
\end{verbatim}
418+
419+
And while the module doesn't directly support parsing strings, it can
420+
easily be done:
421+
422+
\begin{verbatim}
423+
import csv
424+
print csv.reader(['one,two,three'])[0]
425+
\end{verbatim}
426+

0 commit comments

Comments
 (0)