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
soClassto a CSV file.soClasscan also be a SelectResults object, as returned by.select(). If it is a class, all objects will be retrieved, ordered byorderByif given, or the.csvOrderByattribute 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 withselectand pass the class in as the first argument.writeris acsv.writer()object, or a file-like object. If not given, the string of the file will be returned.Uses
connectionas the data source, if given, otherwise the default connection.Columns can be annotated with
.csvTitleattributes, which will form the attributes of the columns, or ‘title’ (secondarily), or if nothing then the column attribute name.If a column has a
.noCSVattribute which is true, then the column will be suppressed.Additionally a class can have an
.extraCSVColumnsattribute, 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
.csvColumnOrderattribute 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
soClasseslist may be a SQLObject class, select result, or(soClass, select)tuple.Each file in the zip will be named after the class name (with
.csvappended), or using the filename in the.csvFilenameattribute.If
fileis given, the zip will be written to that.filemay be a string (a filename) or a file-like object. If not given, a string will be returnd.If
zipis 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).