sqlobject.util.csvexport module

Exports a SQLObject class (possibly annotated) to a CSV file.

sqlobject.util.csvexport.export_csv(soClass, select=None, writer=None, connection=None, orderBy=None)[source]

Export the SQLObject class soClass to a CSV file.

soClass can also be a SelectResults object, as returned by .select(). If it is a class, all objects will be retrieved, ordered by orderBy if given, or the .csvOrderBy attribute if present (but csvOrderBy will only be applied when no select result is given).

You can also pass in select results (or simply a list of instances) in select – if you have a list of objects (not a SelectResults instance, as produced by .select()) then you must pass it in with select and pass the class in as the first argument.

writer is a csv.writer() object, or a file-like object. If not given, the string of the file will be returned.

Uses connection as the data source, if given, otherwise the default connection.

Columns can be annotated with .csvTitle attributes, which will form the attributes of the columns, or ‘title’ (secondarily), or if nothing then the column attribute name.

If a column has a .noCSV attribute which is true, then the column will be suppressed.

Additionally a class can have an .extraCSVColumns attribute, which should be a list of strings/tuples. If a tuple, it should be like (attribute, title), otherwise it is the attribute, which will also be the title. These will be appended to the end of the CSV file; the attribute will be retrieved from instances.

Also a .csvColumnOrder attribute can be on the class, which is the string names of attributes in the order they should be presented.

sqlobject.util.csvexport.export_csv_zip(soClasses, file=None, zip=None, filename_prefix='', connection=None)[source]

Export several SQLObject classes into a .zip file. Each item in the soClasses list may be a SQLObject class, select result, or (soClass, select) tuple.

Each file in the zip will be named after the class name (with .csv appended), or using the filename in the .csvFilename attribute.

If file is given, the zip will be written to that. file may be a string (a filename) or a file-like object. If not given, a string will be returnd.

If zip is given, then the files will be written to that zip file.

All filenames will be prefixed with filename_prefix (which may be a directory name, for instance).