dataimport.py
changeset 8929 b747f2532e03
parent 8926 336e4971dc50
child 8930 6a02be304486
equal deleted inserted replaced
8928:f5b40b66d36e 8929:b747f2532e03
    68 
    68 
    69 import csv
    69 import csv
    70 import sys
    70 import sys
    71 import threading
    71 import threading
    72 import traceback
    72 import traceback
       
    73 import warnings
    73 import cPickle
    74 import cPickle
    74 import os.path as osp
    75 import os.path as osp
    75 import inspect
    76 import inspect
    76 from collections import defaultdict
    77 from collections import defaultdict
    77 from contextlib import contextmanager
    78 from contextlib import contextmanager
   429         # Iterate over the different columns and the different values
   430         # Iterate over the different columns and the different values
   430         # and try to convert them to a correct datatype.
   431         # and try to convert them to a correct datatype.
   431         # If an error is raised, do not continue.
   432         # If an error is raised, do not continue.
   432         formatted_row = []
   433         formatted_row = []
   433         for col in columns:
   434         for col in columns:
   434             if isinstance(row, dict):
   435             try:
   435                 value = row.get(col)
       
   436             elif isinstance(row, (tuple, list)):
       
   437                 value = row[col]
   436                 value = row[col]
   438             else:
   437             except KeyError:
   439                 raise ValueError("Input data of improper type: %s; "
   438                 warnings.warn(u"Column %s is not accessible in row %s" 
   440                                  "expected tuple, list or dict." 
   439                               % (col, row), RuntimeWarning)
   441                                  % type(row).__name__)
   440                 # XXX 'value' set to None so that the import does not end in 
       
   441                 # error. 
       
   442                 # Instead, the extra keys are set to NULL from the 
       
   443                 # database point of view.
       
   444                 value = None
   442             if value is None:
   445             if value is None:
   443                 value = 'NULL'
   446                 value = 'NULL'
   444             elif isinstance(value, (long, int, float)):
   447             elif isinstance(value, (long, int, float)):
   445                 value = str(value)
   448                 value = str(value)
   446             elif isinstance(value, (str, unicode)):
   449             elif isinstance(value, (str, unicode)):