sqlobject.util.csvimport module¶
Import from a CSV file or directory of files.
CSV files should have a header line that lists columns. Headers can
also be appended with :type
to indicate the type of the field.
escaped
is the default, though it can be overridden by the importer.
Supported types:
:python
:- A python expression, run through
eval()
. This can be a security risk, pass inallow_python=False
if you don’t want to allow it. :int
:- Integer
:float
:- Float
:str
:- String
:escaped
:- A string with backslash escapes (note that you don’t put quotation marks around the value)
:base64
:- A base64-encoded string
:date
:- ISO date, like YYYY-MM-DD; this can also be
NOW+days
orNOW-days
:datetime
:- ISO date/time like YYYY-MM-DDTHH:MM:SS (either T or a space can be
used to separate the time, and seconds are optional). This can
also be
NOW+seconds
orNOW-seconds
:bool
:- Converts true/false/yes/no/on/off/1/0 to boolean value
:ref
:- This will be resolved to the ID of the object named in this column (None if the column is empty). @@: Since there’s no ordering, there’s no way to promise the object already exists.
You can also get back references to the objects if you have a special
[name]
column.
Any column named [comment]
or with no name will be ignored.
In any column you can put [default]
to exclude the value and use
whatever default the class wants. [null]
will use NULL.
Lines that begin with [comment]
are ignored.
-
sqlobject.util.csvimport.
load_csv_from_directory
(directory, allow_python=True, default_type='escaped', allow_multiple_classes=True)[source]¶ Load the data from all the files in a directory. Filenames indicate the class, with
general.csv
for data not associated with a class. Return data just likeload_csv
does.This might cause problems on case-insensitive filesystems.
-
sqlobject.util.csvimport.
load_csv
(csvreader, allow_python=True, default_type='escaped', default_class=None, allow_multiple_classes=True)[source]¶ Loads the CSV file, returning a list of dictionaries with types coerced.
-
sqlobject.util.csvimport.
create_data
(data, class_getter, keyorder=None)[source]¶ Create the
data
, which is the return value fromload_csv()
. Classes will be resolved with the callableclass_getter
; or ifclass_getter
is a module then the class names will be attributes of that.Returns a dictionary of
{object_name: object(s)}
, using the names from the[name]
columns (if there are any). If a name is used multiple times, you get a list of objects, not a single object.If
keyorder
is given, then the keys will be retrieved in that order. It can be a list/tuple of names, or a sorting function. If not given andclass_getter
is a module and has asoClasses
function, then that will be used for the order.