~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Doc/library/csv.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
:mod:`csv` --- CSV File Reading and Writing
 
3
===========================================
 
4
 
 
5
.. module:: csv
 
6
   :synopsis: Write and read tabular data to and from delimited files.
 
7
.. sectionauthor:: Skip Montanaro <skip@pobox.com>
 
8
 
 
9
 
 
10
.. index::
 
11
   single: csv
 
12
   pair: data; tabular
 
13
 
 
14
The so-called CSV (Comma Separated Values) format is the most common import and
 
15
export format for spreadsheets and databases.  There is no "CSV standard", so
 
16
the format is operationally defined by the many applications which read and
 
17
write it.  The lack of a standard means that subtle differences often exist in
 
18
the data produced and consumed by different applications.  These differences can
 
19
make it annoying to process CSV files from multiple sources.  Still, while the
 
20
delimiters and quoting characters vary, the overall format is similar enough
 
21
that it is possible to write a single module which can efficiently manipulate
 
22
such data, hiding the details of reading and writing the data from the
 
23
programmer.
 
24
 
 
25
The :mod:`csv` module implements classes to read and write tabular data in CSV
 
26
format.  It allows programmers to say, "write this data in the format preferred
 
27
by Excel," or "read data from this file which was generated by Excel," without
 
28
knowing the precise details of the CSV format used by Excel.  Programmers can
 
29
also describe the CSV formats understood by other applications or define their
 
30
own special-purpose CSV formats.
 
31
 
 
32
The :mod:`csv` module's :class:`reader` and :class:`writer` objects read and
 
33
write sequences.  Programmers can also read and write data in dictionary form
 
34
using the :class:`DictReader` and :class:`DictWriter` classes.
 
35
 
 
36
.. note::
 
37
 
 
38
   This version of the :mod:`csv` module doesn't support Unicode input.  Also,
 
39
   there are currently some issues regarding ASCII NUL characters.  Accordingly,
 
40
   all input should be UTF-8 or printable ASCII to be safe; see the examples in
 
41
   section :ref:`csv-examples`. These restrictions will be removed in the future.
 
42
 
 
43
 
 
44
.. seealso::
 
45
 
 
46
   :pep:`305` - CSV File API
 
47
      The Python Enhancement Proposal which proposed this addition to Python.
 
48
 
 
49
 
 
50
.. _csv-contents:
 
51
 
 
52
Module Contents
 
53
---------------
 
54
 
 
55
The :mod:`csv` module defines the following functions:
 
56
 
 
57
 
 
58
.. function:: reader(csvfile[, dialect='excel'][, fmtparam])
 
59
 
 
60
   Return a reader object which will iterate over lines in the given *csvfile*.
 
61
   *csvfile* can be any object which supports the :term:`iterator` protocol and returns a
 
62
   string each time its :meth:`next` method is called --- file objects and list
 
63
   objects are both suitable.   If *csvfile* is a file object, it must be opened
 
64
   with the 'b' flag on platforms where that makes a difference.  An optional
 
65
   *dialect* parameter can be given which is used to define a set of parameters
 
66
   specific to a particular CSV dialect.  It may be an instance of a subclass of
 
67
   the :class:`Dialect` class or one of the strings returned by the
 
68
   :func:`list_dialects` function.  The other optional *fmtparam* keyword arguments
 
69
   can be given to override individual formatting parameters in the current
 
70
   dialect.  For full details about the dialect and formatting parameters, see
 
71
   section :ref:`csv-fmt-params`.
 
72
 
 
73
   All data read are returned as strings.  No automatic data type conversion is
 
74
   performed.
 
75
 
 
76
   The parser is quite strict with respect to multi-line quoted fields. Previously,
 
77
   if a line ended within a quoted field without a terminating newline character, a
 
78
   newline would be inserted into the returned field. This behavior caused problems
 
79
   when reading files which contained carriage return characters within fields.
 
80
   The behavior was changed to return the field without inserting newlines. As a
 
81
   consequence, if newlines embedded within fields are important, the input should
 
82
   be split into lines in a manner which preserves the newline characters.
 
83
 
 
84
   A short usage example::
 
85
 
 
86
      >>> import csv
 
87
      >>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
 
88
      >>> for row in spamReader:
 
89
      ...     print(', '.join(row))
 
90
      Spam, Spam, Spam, Spam, Spam, Baked Beans
 
91
      Spam, Lovely Spam, Wonderful Spam
 
92
 
 
93
 
 
94
.. function:: writer(csvfile[, dialect='excel'][, fmtparam])
 
95
 
 
96
   Return a writer object responsible for converting the user's data into delimited
 
97
   strings on the given file-like object.  *csvfile* can be any object with a
 
98
   :func:`write` method.  If *csvfile* is a file object, it must be opened with the
 
99
   'b' flag on platforms where that makes a difference.  An optional *dialect*
 
100
   parameter can be given which is used to define a set of parameters specific to a
 
101
   particular CSV dialect.  It may be an instance of a subclass of the
 
102
   :class:`Dialect` class or one of the strings returned by the
 
103
   :func:`list_dialects` function.  The other optional *fmtparam* keyword arguments
 
104
   can be given to override individual formatting parameters in the current
 
105
   dialect.  For full details about the dialect and formatting parameters, see
 
106
   section :ref:`csv-fmt-params`. To make it
 
107
   as easy as possible to interface with modules which implement the DB API, the
 
108
   value :const:`None` is written as the empty string.  While this isn't a
 
109
   reversible transformation, it makes it easier to dump SQL NULL data values to
 
110
   CSV files without preprocessing the data returned from a ``cursor.fetch*`` call.
 
111
   All other non-string data are stringified with :func:`str` before being written.
 
112
 
 
113
   A short usage example::
 
114
 
 
115
      >>> import csv
 
116
      >>> spamWriter = csv.writer(open('eggs.csv', 'w'), delimiter=' ',
 
117
      ...                         quotechar='|', quoting=QUOTE_MINIMAL)
 
118
      >>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
 
119
      >>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
 
120
 
 
121
 
 
122
.. function:: register_dialect(name[, dialect][, fmtparam])
 
123
 
 
124
   Associate *dialect* with *name*.  *name* must be a string. The
 
125
   dialect can be specified either by passing a sub-class of :class:`Dialect`, or
 
126
   by *fmtparam* keyword arguments, or both, with keyword arguments overriding
 
127
   parameters of the dialect. For full details about the dialect and formatting
 
128
   parameters, see section :ref:`csv-fmt-params`.
 
129
 
 
130
 
 
131
.. function:: unregister_dialect(name)
 
132
 
 
133
   Delete the dialect associated with *name* from the dialect registry.  An
 
134
   :exc:`Error` is raised if *name* is not a registered dialect name.
 
135
 
 
136
 
 
137
.. function:: get_dialect(name)
 
138
 
 
139
   Return the dialect associated with *name*.  An :exc:`Error` is raised if
 
140
   *name* is not a registered dialect name.  This function returns an immutable
 
141
   :class:`Dialect`.
 
142
 
 
143
.. function:: list_dialects()
 
144
 
 
145
   Return the names of all registered dialects.
 
146
 
 
147
 
 
148
.. function:: field_size_limit([new_limit])
 
149
 
 
150
   Returns the current maximum field size allowed by the parser. If *new_limit* is
 
151
   given, this becomes the new limit.
 
152
 
 
153
 
 
154
The :mod:`csv` module defines the following classes:
 
155
 
 
156
.. class:: DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])
 
157
 
 
158
   Create an object which operates like a regular reader but maps the information
 
159
   read into a dict whose keys are given by the optional  *fieldnames* parameter.
 
160
   If the *fieldnames* parameter is omitted, the values in the first row of the
 
161
   *csvfile* will be used as the fieldnames. If the row read has fewer fields than
 
162
   the fieldnames sequence, the value of *restval* will be used as the default
 
163
   value.  If the row read has more fields than the fieldnames sequence, the
 
164
   remaining data is added as a sequence keyed by the value of *restkey*.  If the
 
165
   row read has fewer fields than the fieldnames sequence, the remaining keys take
 
166
   the value of the optional *restval* parameter.  Any other optional or keyword
 
167
   arguments are passed to the underlying :class:`reader` instance.
 
168
 
 
169
 
 
170
.. class:: DictWriter(csvfile, fieldnames[, restval=''[, extrasaction='raise'[, dialect='excel'[, *args, **kwds]]]])
 
171
 
 
172
   Create an object which operates like a regular writer but maps dictionaries onto
 
173
   output rows.  The *fieldnames* parameter identifies the order in which values in
 
174
   the dictionary passed to the :meth:`writerow` method are written to the
 
175
   *csvfile*.  The optional *restval* parameter specifies the value to be written
 
176
   if the dictionary is missing a key in *fieldnames*.  If the dictionary passed to
 
177
   the :meth:`writerow` method contains a key not found in *fieldnames*, the
 
178
   optional *extrasaction* parameter indicates what action to take.  If it is set
 
179
   to ``'raise'`` a :exc:`ValueError` is raised.  If it is set to ``'ignore'``,
 
180
   extra values in the dictionary are ignored.  Any other optional or keyword
 
181
   arguments are passed to the underlying :class:`writer` instance.
 
182
 
 
183
   Note that unlike the :class:`DictReader` class, the *fieldnames* parameter of
 
184
   the :class:`DictWriter` is not optional.  Since Python's :class:`dict` objects
 
185
   are not ordered, there is not enough information available to deduce the order
 
186
   in which the row should be written to the *csvfile*.
 
187
 
 
188
 
 
189
.. class:: Dialect
 
190
 
 
191
   The :class:`Dialect` class is a container class relied on primarily for its
 
192
   attributes, which are used to define the parameters for a specific
 
193
   :class:`reader` or :class:`writer` instance.
 
194
 
 
195
 
 
196
.. class:: excel()
 
197
 
 
198
   The :class:`excel` class defines the usual properties of an Excel-generated CSV
 
199
   file.  It is registered with the dialect name ``'excel'``.
 
200
 
 
201
 
 
202
.. class:: excel_tab()
 
203
 
 
204
   The :class:`excel_tab` class defines the usual properties of an Excel-generated
 
205
   TAB-delimited file.  It is registered with the dialect name ``'excel-tab'``.
 
206
 
 
207
 
 
208
.. class:: Sniffer()
 
209
 
 
210
   The :class:`Sniffer` class is used to deduce the format of a CSV file.
 
211
 
 
212
   The :class:`Sniffer` class provides two methods:
 
213
 
 
214
   .. method:: sniff(sample[, delimiters=None])
 
215
 
 
216
      Analyze the given *sample* and return a :class:`Dialect` subclass
 
217
      reflecting the parameters found.  If the optional *delimiters* parameter
 
218
      is given, it is interpreted as a string containing possible valid
 
219
      delimiter characters.
 
220
 
 
221
 
 
222
   .. method:: has_header(sample)
 
223
 
 
224
      Analyze the sample text (presumed to be in CSV format) and return
 
225
      :const:`True` if the first row appears to be a series of column headers.
 
226
 
 
227
An example for :class:`Sniffer` use::
 
228
 
 
229
   csvfile = open("example.csv")
 
230
   dialect = csv.Sniffer().sniff(csvfile.read(1024))
 
231
   csvfile.seek(0)
 
232
   reader = csv.reader(csvfile, dialect)
 
233
   # ... process CSV file contents here ...
 
234
 
 
235
 
 
236
The :mod:`csv` module defines the following constants:
 
237
 
 
238
.. data:: QUOTE_ALL
 
239
 
 
240
   Instructs :class:`writer` objects to quote all fields.
 
241
 
 
242
 
 
243
.. data:: QUOTE_MINIMAL
 
244
 
 
245
   Instructs :class:`writer` objects to only quote those fields which contain
 
246
   special characters such as *delimiter*, *quotechar* or any of the characters in
 
247
   *lineterminator*.
 
248
 
 
249
 
 
250
.. data:: QUOTE_NONNUMERIC
 
251
 
 
252
   Instructs :class:`writer` objects to quote all non-numeric fields.
 
253
 
 
254
   Instructs the reader to convert all non-quoted fields to type *float*.
 
255
 
 
256
 
 
257
.. data:: QUOTE_NONE
 
258
 
 
259
   Instructs :class:`writer` objects to never quote fields.  When the current
 
260
   *delimiter* occurs in output data it is preceded by the current *escapechar*
 
261
   character.  If *escapechar* is not set, the writer will raise :exc:`Error` if
 
262
   any characters that require escaping are encountered.
 
263
 
 
264
   Instructs :class:`reader` to perform no special processing of quote characters.
 
265
 
 
266
The :mod:`csv` module defines the following exception:
 
267
 
 
268
 
 
269
.. exception:: Error
 
270
 
 
271
   Raised by any of the functions when an error is detected.
 
272
 
 
273
 
 
274
.. _csv-fmt-params:
 
275
 
 
276
Dialects and Formatting Parameters
 
277
----------------------------------
 
278
 
 
279
To make it easier to specify the format of input and output records, specific
 
280
formatting parameters are grouped together into dialects.  A dialect is a
 
281
subclass of the :class:`Dialect` class having a set of specific methods and a
 
282
single :meth:`validate` method.  When creating :class:`reader` or
 
283
:class:`writer` objects, the programmer can specify a string or a subclass of
 
284
the :class:`Dialect` class as the dialect parameter.  In addition to, or instead
 
285
of, the *dialect* parameter, the programmer can also specify individual
 
286
formatting parameters, which have the same names as the attributes defined below
 
287
for the :class:`Dialect` class.
 
288
 
 
289
Dialects support the following attributes:
 
290
 
 
291
 
 
292
.. attribute:: Dialect.delimiter
 
293
 
 
294
   A one-character string used to separate fields.  It defaults to ``','``.
 
295
 
 
296
 
 
297
.. attribute:: Dialect.doublequote
 
298
 
 
299
   Controls how instances of *quotechar* appearing inside a field should be
 
300
   themselves be quoted.  When :const:`True`, the character is doubled. When
 
301
   :const:`False`, the *escapechar* is used as a prefix to the *quotechar*.  It
 
302
   defaults to :const:`True`.
 
303
 
 
304
   On output, if *doublequote* is :const:`False` and no *escapechar* is set,
 
305
   :exc:`Error` is raised if a *quotechar* is found in a field.
 
306
 
 
307
 
 
308
.. attribute:: Dialect.escapechar
 
309
 
 
310
   A one-character string used by the writer to escape the *delimiter* if *quoting*
 
311
   is set to :const:`QUOTE_NONE` and the *quotechar* if *doublequote* is
 
312
   :const:`False`. On reading, the *escapechar* removes any special meaning from
 
313
   the following character. It defaults to :const:`None`, which disables escaping.
 
314
 
 
315
 
 
316
.. attribute:: Dialect.lineterminator
 
317
 
 
318
   The string used to terminate lines produced by the :class:`writer`. It defaults
 
319
   to ``'\r\n'``.
 
320
 
 
321
   .. note::
 
322
 
 
323
      The :class:`reader` is hard-coded to recognise either ``'\r'`` or ``'\n'`` as
 
324
      end-of-line, and ignores *lineterminator*. This behavior may change in the
 
325
      future.
 
326
 
 
327
 
 
328
.. attribute:: Dialect.quotechar
 
329
 
 
330
   A one-character string used to quote fields containing special characters, such
 
331
   as the *delimiter* or *quotechar*, or which contain new-line characters.  It
 
332
   defaults to ``'"'``.
 
333
 
 
334
 
 
335
.. attribute:: Dialect.quoting
 
336
 
 
337
   Controls when quotes should be generated by the writer and recognised by the
 
338
   reader.  It can take on any of the :const:`QUOTE_\*` constants (see section
 
339
   :ref:`csv-contents`) and defaults to :const:`QUOTE_MINIMAL`.
 
340
 
 
341
 
 
342
.. attribute:: Dialect.skipinitialspace
 
343
 
 
344
   When :const:`True`, whitespace immediately following the *delimiter* is ignored.
 
345
   The default is :const:`False`.
 
346
 
 
347
 
 
348
Reader Objects
 
349
--------------
 
350
 
 
351
Reader objects (:class:`DictReader` instances and objects returned by the
 
352
:func:`reader` function) have the following public methods:
 
353
 
 
354
 
 
355
.. method:: csvreader.next()
 
356
 
 
357
   Return the next row of the reader's iterable object as a list, parsed according
 
358
   to the current dialect.
 
359
 
 
360
Reader objects have the following public attributes:
 
361
 
 
362
 
 
363
.. attribute:: csvreader.dialect
 
364
 
 
365
   A read-only description of the dialect in use by the parser.
 
366
 
 
367
 
 
368
.. attribute:: csvreader.line_num
 
369
 
 
370
   The number of lines read from the source iterator. This is not the same as the
 
371
   number of records returned, as records can span multiple lines.
 
372
 
 
373
 
 
374
 
 
375
DictReader objects have the following public attribute:
 
376
 
 
377
 
 
378
.. attribute:: csvreader.fieldnames
 
379
 
 
380
   If not passed as a parameter when creating the object, this attribute is
 
381
   initialized upon first access or when the first record is read from the
 
382
   file.
 
383
 
 
384
 
 
385
 
 
386
Writer Objects
 
387
--------------
 
388
 
 
389
:class:`Writer` objects (:class:`DictWriter` instances and objects returned by
 
390
the :func:`writer` function) have the following public methods.  A *row* must be
 
391
a sequence of strings or numbers for :class:`Writer` objects and a dictionary
 
392
mapping fieldnames to strings or numbers (by passing them through :func:`str`
 
393
first) for :class:`DictWriter` objects.  Note that complex numbers are written
 
394
out surrounded by parens. This may cause some problems for other programs which
 
395
read CSV files (assuming they support complex numbers at all).
 
396
 
 
397
 
 
398
.. method:: csvwriter.writerow(row)
 
399
 
 
400
   Write the *row* parameter to the writer's file object, formatted according to
 
401
   the current dialect.
 
402
 
 
403
 
 
404
.. method:: csvwriter.writerows(rows)
 
405
 
 
406
   Write all the *rows* parameters (a list of *row* objects as described above) to
 
407
   the writer's file object, formatted according to the current dialect.
 
408
 
 
409
Writer objects have the following public attribute:
 
410
 
 
411
 
 
412
.. attribute:: csvwriter.dialect
 
413
 
 
414
   A read-only description of the dialect in use by the writer.
 
415
 
 
416
 
 
417
.. _csv-examples:
 
418
 
 
419
Examples
 
420
--------
 
421
 
 
422
The simplest example of reading a CSV file::
 
423
 
 
424
   import csv
 
425
   reader = csv.reader(open("some.csv", "rb"))
 
426
   for row in reader:
 
427
       print(row)
 
428
 
 
429
Reading a file with an alternate format::
 
430
 
 
431
   import csv
 
432
   reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
 
433
   for row in reader:
 
434
       print(row)
 
435
 
 
436
The corresponding simplest possible writing example is::
 
437
 
 
438
   import csv
 
439
   writer = csv.writer(open("some.csv", "wb"))
 
440
   writer.writerows(someiterable)
 
441
 
 
442
Registering a new dialect::
 
443
 
 
444
   import csv
 
445
 
 
446
   csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
 
447
 
 
448
   reader = csv.reader(open("passwd", "rb"), 'unixpwd')
 
449
 
 
450
A slightly more advanced use of the reader --- catching and reporting errors::
 
451
 
 
452
   import csv, sys
 
453
   filename = "some.csv"
 
454
   reader = csv.reader(open(filename, "rb"))
 
455
   try:
 
456
       for row in reader:
 
457
           print(row)
 
458
   except csv.Error as e:
 
459
       sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
 
460
 
 
461
And while the module doesn't directly support parsing strings, it can easily be
 
462
done::
 
463
 
 
464
   import csv
 
465
   for row in csv.reader(['one,two,three']):
 
466
       print(row)
 
467
 
 
468
The :mod:`csv` module doesn't directly support reading and writing Unicode, but
 
469
it is 8-bit-clean save for some problems with ASCII NUL characters.  So you can
 
470
write functions or classes that handle the encoding and decoding for you as long
 
471
as you avoid encodings like UTF-16 that use NULs.  UTF-8 is recommended.
 
472
 
 
473
:func:`unicode_csv_reader` below is a :term:`generator` that wraps :class:`csv.reader`
 
474
to handle Unicode CSV data (a list of Unicode strings).  :func:`utf_8_encoder`
 
475
is a :term:`generator` that encodes the Unicode strings as UTF-8, one string (or row) at
 
476
a time.  The encoded strings are parsed by the CSV reader, and
 
477
:func:`unicode_csv_reader` decodes the UTF-8-encoded cells back into Unicode::
 
478
 
 
479
   import csv
 
480
 
 
481
   def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
 
482
       # csv.py doesn't do Unicode; encode temporarily as UTF-8:
 
483
       csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
 
484
                               dialect=dialect, **kwargs)
 
485
       for row in csv_reader:
 
486
           # decode UTF-8 back to Unicode, cell by cell:
 
487
           yield [unicode(cell, 'utf-8') for cell in row]
 
488
 
 
489
   def utf_8_encoder(unicode_csv_data):
 
490
       for line in unicode_csv_data:
 
491
           yield line.encode('utf-8')
 
492
 
 
493
For all other encodings the following :class:`UnicodeReader` and
 
494
:class:`UnicodeWriter` classes can be used. They take an additional *encoding*
 
495
parameter in their constructor and make sure that the data passes the real
 
496
reader or writer encoded as UTF-8::
 
497
 
 
498
   import csv, codecs, io
 
499
 
 
500
   class UTF8Recoder:
 
501
       """
 
502
       Iterator that reads an encoded stream and reencodes the input to UTF-8
 
503
       """
 
504
       def __init__(self, f, encoding):
 
505
           self.reader = codecs.getreader(encoding)(f)
 
506
 
 
507
       def __iter__(self):
 
508
           return self
 
509
 
 
510
       def __next__(self):
 
511
           return next(self.reader).encode("utf-8")
 
512
 
 
513
   class UnicodeReader:
 
514
       """
 
515
       A CSV reader which will iterate over lines in the CSV file "f",
 
516
       which is encoded in the given encoding.
 
517
       """
 
518
 
 
519
       def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
 
520
           f = UTF8Recoder(f, encoding)
 
521
           self.reader = csv.reader(f, dialect=dialect, **kwds)
 
522
 
 
523
       def __next__(self):
 
524
           row = next(self.reader)
 
525
           return [unicode(s, "utf-8") for s in row]
 
526
 
 
527
       def __iter__(self):
 
528
           return self
 
529
 
 
530
   class UnicodeWriter:
 
531
       """
 
532
       A CSV writer which will write rows to CSV file "f",
 
533
       which is encoded in the given encoding.
 
534
       """
 
535
 
 
536
       def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
 
537
           # Redirect output to a queue
 
538
           self.queue = io.StringIO()
 
539
           self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
 
540
           self.stream = f
 
541
           self.encoder = codecs.getincrementalencoder(encoding)()
 
542
 
 
543
       def writerow(self, row):
 
544
           self.writer.writerow([s.encode("utf-8") for s in row])
 
545
           # Fetch UTF-8 output from the queue ...
 
546
           data = self.queue.getvalue()
 
547
           data = data.decode("utf-8")
 
548
           # ... and reencode it into the target encoding
 
549
           data = self.encoder.encode(data)
 
550
           # write to the target stream
 
551
           self.stream.write(data)
 
552
           # empty queue
 
553
           self.queue.truncate(0)
 
554
 
 
555
       def writerows(self, rows):
 
556
           for row in rows:
 
557
               self.writerow(row)
 
558