dataimport.py
changeset 9902 62c586f32f93
parent 9901 161ec913aeec
child 9904 2205c5e1edc2
equal deleted inserted replaced
9901:161ec913aeec 9902:62c586f32f93
   485     (datetime, _copyfrom_buffer_convert_datetime),
   485     (datetime, _copyfrom_buffer_convert_datetime),
   486     (date, _copyfrom_buffer_convert_date),
   486     (date, _copyfrom_buffer_convert_date),
   487     (time, _copyfrom_buffer_convert_time),
   487     (time, _copyfrom_buffer_convert_time),
   488 ]
   488 ]
   489 
   489 
   490 def _create_copyfrom_buffer(data, columns, **convert_opts):
   490 def _create_copyfrom_buffer(data, columns=None, **convert_opts):
   491     """
   491     """
   492     Create a StringIO buffer for 'COPY FROM' command.
   492     Create a StringIO buffer for 'COPY FROM' command.
   493     Deals with Unicode, Int, Float, Date... (see ``converters``)
   493     Deals with Unicode, Int, Float, Date... (see ``converters``)
   494 
   494 
   495     :data: a sequence/dict of tuples
   495     :data: a sequence/dict of tuples
   497     :converter_opts: keyword arguements given to converters
   497     :converter_opts: keyword arguements given to converters
   498     """
   498     """
   499     # Create a list rather than directly create a StringIO
   499     # Create a list rather than directly create a StringIO
   500     # to correctly write lines separated by '\n' in a single step
   500     # to correctly write lines separated by '\n' in a single step
   501     rows = []
   501     rows = []
   502     if isinstance(data[0], (tuple, list)):
   502     if columns is None:
   503         columns = range(len(data[0]))
   503         if isinstance(data[0], (tuple, list)):
       
   504             columns = range(len(data[0]))
       
   505         elif isinstance(data[0], dict):
       
   506             columns = data[0].keys()
       
   507         else:
       
   508             raise ValueError('Could not get columns: you must provide columns.')
   504     for row in data:
   509     for row in data:
   505         # Iterate over the different columns and the different values
   510         # Iterate over the different columns and the different values
   506         # and try to convert them to a correct datatype.
   511         # and try to convert them to a correct datatype.
   507         # If an error is raised, do not continue.
   512         # If an error is raised, do not continue.
   508         formatted_row = []
   513         formatted_row = []