rattail.csvutil
¶
CSV File Utilities
Contains various utilities relating to CSV file processing.
Note
This module is named csvutil
instead of csv
primarily as a
workaround to the problem of PythonService.exe
insisting on doing
relative imports.
-
class
rattail.csvutil.
DictWriter
(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)[source]¶ Convenience implementation of
csv.DictWriter
.This exists only to provide the
writeheader()
method on Python 2.6.
-
class
rattail.csvutil.
UTF8Recoder
(fileobj, encoding)[source]¶ Iterator that reads an encoded stream and reencodes the input to UTF-8.
Note
This class was stolen from the Python 2.7 documentation.
-
class
rattail.csvutil.
UnicodeDictReader
(fileobj, dialect=<class csv.excel>, encoding=u'utf_8', **kwargs)[source]¶ A CSV Dict reader which will iterate over lines in a CSV file, which is encoded in the given encoding.
-
class
rattail.csvutil.
UnicodeDictWriter
(f, fields, dialect=u'excel', encoding=u'utf_8', **kwds)[source]¶ A
DictWriter
-ish class which accepts row data as Unicode and can write to the file with any encoding.Note
This logic was stolen from a Django snippet. The original docstring from this snippet follows (“sic” applies here; our logic uses ‘utf_8’ encoding and regular ‘excel’ dialect by default):
A CSV writer that produces Excel-compatibly CSV files from unicode data. Uses UTF-16 and tabs as delimeters - it turns out this is the only way to get unicode data in to Excel using CSV.
Usage example:
fp = open('my-file.csv', 'wb') writer = UnicodeDictWriter(fp, ['name', 'age', 'shoesize']) writer.writerows([ {'name': u'Bob', 'age': 22, 'shoesize': 7}, {'name': u'Sue', 'age': 28, 'shoesize': 6}, {'name': u'Ben', 'age': 31, 'shoesize': 8}, # Ã is LATIN CAPITAL LETTER A WITH MACRON {'name': 'Ädam'.decode('utf8'), 'age': 11, 'shoesize': 4}, ]) fp.close()
Initially derived from http://docs.python.org/lib/csv-examples.html