@@ -30,10 +30,12 @@ \section{\module{csv} --- CSV File Reading and Writing}
3030write sequences. Programmers can also read and write data in dictionary
3131form using the \class {DictReader} and \class {DictWriter} classes.
3232
33- \note {The first version of the \module {csv} module doesn't support Unicode
34- input. Also, there are currently some issues regarding \ASCII {} NUL
35- characters. Accordingly, all input should generally be printable \ASCII {}
36- to be safe. These restrictions will be removed in the future.}
33+ \begin {notice }
34+ This version of the \module {csv} module doesn't support Unicode
35+ input. Also, there are currently some issues regarding \ASCII {} NUL
36+ characters. Accordingly, all input should generally be printable
37+ \ASCII {} to be safe. These restrictions will be removed in the future.
38+ \end {notice }
3739
3840\begin {seealso }
3941% \seemodule{array}{Arrays of uniformly types numeric values.}
@@ -45,7 +47,6 @@ \section{\module{csv} --- CSV File Reading and Writing}
4547
4648\subsection {Module Contents }
4749
48-
4950The \module {csv} module defines the following functions:
5051
5152\begin {funcdesc }{reader}{csvfile\optional {,
@@ -112,8 +113,8 @@ \subsection{Module Contents}
112113The \module {csv} module defines the following classes:
113114
114115\begin {classdesc }{DictReader}{csvfile, fieldnames\optional {,
115- restkey=\code {None}\optional {,
116- restval=\code {None}\optional {,
116+ restkey=\constant {None}\optional {,
117+ restval=\constant {None}\optional {,
117118 dialect=\code {'excel'}\optional {,
118119 fmtparam}}}}}
119120Create an object which operates like a regular reader but maps the
@@ -145,52 +146,31 @@ \subsection{Module Contents}
145146parameters are interpreted as for regular writers.
146147\end {classdesc }
147148
148-
149149\begin {classdesc* }{Dialect}{}
150150The \class {Dialect} class is a container class relied on primarily for its
151151attributes, which are used to define the parameters for a specific
152- \class {reader} or \class {writer} instance. Dialect objects support the
153- following data attributes:
154-
155- \begin {memberdesc }[string]{delimiter}
156- A one-character string used to separate fields. It defaults to \code {","}.
157- \end {memberdesc }
158-
159- \begin {memberdesc }[boolean]{doublequote}
160- Controls how instances of \var {quotechar} appearing inside a field should be
161- themselves be quoted. When \constant {True}, the character is doubledd.
162- When \constant {False}, the \var {escapechar} must be a one-character string
163- which is used as a prefix to the \var {quotechar}. It defaults to
164- \constant {True}.
165- \end {memberdesc }
166-
167- \begin {memberdesc }{escapechar}
168- A one-character string used to escape the \var {delimiter} if \var {quoting}
169- is set to \constant {QUOTE_NONE}. It defaults to \constant {None}.
170- \end {memberdesc }
152+ \class {reader} or \class {writer} instance.
153+ \end {classdesc* }
171154
172- \begin {memberdesc }[string]{lineterminator}
173- The string used to terminate lines in the CSV file. It defaults to
174- \code {"\e r\e n"}.
175- \end {memberdesc }
155+ \begin {classdesc }{Sniffer}{\optional {sample=16384}}
156+ The \class {Sniffer} class is used to deduce the format of a CSV file. The
157+ optional \var {sample} argument to the constructor specifies the number of
158+ bytes to use when determining Dialect parameters.
159+ \end {classdesc }
176160
177- \begin {memberdesc }[string]{quotechar}
178- A one-character string used to quote elements containing the \var {delimiter}
179- or which start with the \var {quotechar}. It defaults to \code {'"'}.
180- \end {memberdesc }
161+ The \class {Sniffer} class provides a single method:
181162
182- \begin {memberdesc }[integer]{quoting}
183- Controls when quotes should be generated by the writer. It can take on any
184- of the \code {QUOTE_*} constants defined below and defaults to
185- \constant {QUOTE_MINIMAL}.
186- \end {memberdesc }
163+ \begin {methoddesc }{sniff}{fileobj}
164+ Analyze the next chunk of \var {fileobj} and return a \class {Dialect} subclass
165+ reflecting the parameters found.
166+ \end {methoddesc }
187167
188- \begin {memberdesc }[boolean]{skipinitialspace}
189- When \constant {True}, whitespace immediately following the \var {delimiter}
190- is ignored. The default is \constant {False}.
191- \end {memberdesc }
168+ \begin {methoddesc }{has_header}{sample}
169+ Analyze the sample text (presumed to be in CSV format) and return
170+ \constant {True} if the first row appears to be a series of column
171+ headers.
172+ \end {methoddesc }
192173
193- \end {classdesc* }
194174
195175The \module {csv} module defines the following constants:
196176
@@ -223,7 +203,7 @@ \subsection{Module Contents}
223203\end {excdesc }
224204
225205
226- \subsection {Dialects and Formatting Parameters\label {fmt-params } }
206+ \subsection {Dialects and Formatting Parameters\label {csv- fmt-params } }
227207
228208To make it easier to specify the format of input and output records,
229209specific formatting parameters are grouped together into dialects. A
@@ -235,65 +215,90 @@ \subsection{Dialects and Formatting Parameters\label{fmt-params}}
235215individual formatting parameters, which have the same names as the
236216attributes defined above for the \class {Dialect} class.
237217
218+ Dialects support the following attributes:
219+
220+ \begin {memberdesc }[Dialect]{delimiter}
221+ A one-character string used to separate fields. It defaults to \code {','}.
222+ \end {memberdesc }
223+
224+ \begin {memberdesc }[Dialect]{doublequote}
225+ Controls how instances of \var {quotechar} appearing inside a field should be
226+ themselves be quoted. When \constant {True}, the character is doubledd.
227+ When \constant {False}, the \var {escapechar} must be a one-character string
228+ which is used as a prefix to the \var {quotechar}. It defaults to
229+ \constant {True}.
230+ \end {memberdesc }
231+
232+ \begin {memberdesc }[Dialect]{escapechar}
233+ A one-character string used to escape the \var {delimiter} if \var {quoting}
234+ is set to \constant {QUOTE_NONE}. It defaults to \constant {None}.
235+ \end {memberdesc }
236+
237+ \begin {memberdesc }[Dialect]{lineterminator}
238+ The string used to terminate lines in the CSV file. It defaults to
239+ \code {'\e r\e n'}.
240+ \end {memberdesc }
241+
242+ \begin {memberdesc }[Dialect]{quotechar}
243+ A one-character string used to quote elements containing the \var {delimiter}
244+ or which start with the \var {quotechar}. It defaults to \code {'"'}.
245+ \end {memberdesc }
246+
247+ \begin {memberdesc }[Dialect]{quoting}
248+ Controls when quotes should be generated by the writer. It can take on any
249+ of the \constant {QUOTE_*} constants defined below and defaults to
250+ \constant {QUOTE_MINIMAL}.
251+ \end {memberdesc }
252+
253+ \begin {memberdesc }[Dialect]{skipinitialspace}
254+ When \constant {True}, whitespace immediately following the \var {delimiter}
255+ is ignored. The default is \constant {False}.
256+ \end {memberdesc }
257+
238258
239259\subsection {Reader Objects }
240260
241- \class {DictReader} and \var {reader} objects have the following public
242- methods:
261+ Reader objects ( \class {DictReader} instances and objects returned by
262+ the \function {reader()}function) have the following public methods:
243263
244- \begin {methoddesc }{next}{}
264+ \begin {methoddesc }[csv reader] {next}{}
245265Return the next row of the reader's iterable object as a list, parsed
246266according to the current dialect.
247267\end {methoddesc }
248268
249269
250270\subsection {Writer Objects }
251271
252- \class {DictWriter} and \var {writer} objects have the following public
253- methods:
272+ Writer objects ( \class {DictWriter} instances and objects returned by
273+ the \function {writer()} function) have the following public methods:
254274
255- \begin {methoddesc }{writerow}{row}
275+ \begin {methoddesc }[csv writer] {writerow}{row}
256276Write the \var {row} parameter to the writer's file object, formatted
257277according to the current dialect.
258278\end {methoddesc }
259279
260- \begin {methoddesc }{writerows}{rows}
280+ \begin {methoddesc }[csv writer] {writerows}{rows}
261281Write all the \var {rows} parameters to the writer's file object, formatted
262282according to the current dialect.
263283\end {methoddesc }
264284
265285
266- \begin {classdesc }{Sniffer}{}
267-
268- The \class {Sniffer} class is used to deduce the format of a CSV file.
269-
270- \begin {methoddesc }{sniff}{sample}
271- Analyze the sample text (presumed to be in CSV format) and return a
272- {}\class {Dialect} class reflecting the parameters found.
273- \end {methoddesc }
274-
275- \begin {methoddesc }{has_header}{sample}
276- Analyze the sample text (presumed to be in CSV format) and return
277- {}\code {True} if the first row appears to be a series of column headers.
278- \end {methoddesc }
279- \end {classdesc }
280-
281286\subsection {Examples }
282287
283288The `` Hello, world'' of csv reading is
284289
285290\begin {verbatim }
286- import csv
287- reader = csv.reader(file("some.csv"))
288- for row in reader:
289- print row
291+ import csv
292+ reader = csv.reader(file("some.csv"))
293+ for row in reader:
294+ print row
290295\end {verbatim }
291296
292297The corresponding simplest possible writing example is
293298
294299\begin {verbatim }
295- import csv
296- writer = csv.writer(file("some.csv", "w"))
297- for row in someiterable:
298- writer.writerow(row)
300+ import csv
301+ writer = csv.writer(file("some.csv", "w"))
302+ for row in someiterable:
303+ writer.writerow(row)
299304\end {verbatim }
0 commit comments