dataimport.py
changeset 9900 9c7de09a6648
parent 9899 2918ef1e3199
child 9901 161ec913aeec
equal deleted inserted replaced
9899:2918ef1e3199 9900:9c7de09a6648
   448     for _char in (u'\t', u'\r', u'\n'):
   448     for _char in (u'\t', u'\r', u'\n'):
   449         if _char in value:
   449         if _char in value:
   450             # If a replace_sep is given, replace
   450             # If a replace_sep is given, replace
   451             # the separator
   451             # the separator
   452             # (and thus avoid empty buffer)
   452             # (and thus avoid empty buffer)
   453             if replace_sep is not None:
   453             if replace_sep is None:
   454                 value = value.replace(_char, replace_sep)
   454                 raise ValueError('conflicting separator: '
   455             else:
   455                                  'you must provide the replace_sep option')
   456                 return
   456             value = value.replace(_char, replace_sep)
   457         value = value.replace('\\', r'\\')
   457         value = value.replace('\\', r'\\')
   458     if isinstance(value, unicode):
   458     if isinstance(value, unicode):
   459         value = value.encode(encoding)
   459         value = value.encode(encoding)
   460     return value
   460     return value
   461 
   461 
   509                 # database point of view.
   509                 # database point of view.
   510                 value = None
   510                 value = None
   511             for types, converter in _COPYFROM_BUFFER_CONVERTERS:
   511             for types, converter in _COPYFROM_BUFFER_CONVERTERS:
   512                 if isinstance(value, types):
   512                 if isinstance(value, types):
   513                     value = converter(value, **convert_opts)
   513                     value = converter(value, **convert_opts)
   514                     if value is None:
       
   515                         return None
       
   516                     break
   514                     break
   517             else:
   515             else:
   518                 return None
   516                 raise ValueError("Unsupported value type %s" % type(value))
   519             # We push the value to the new formatted row
   517             # We push the value to the new formatted row
   520             # if the value is not None and could be converted to a string.
   518             # if the value is not None and could be converted to a string.
   521             formatted_row.append(value)
   519             formatted_row.append(value)
   522         rows.append('\t'.join(formatted_row))
   520         rows.append('\t'.join(formatted_row))
   523     return StringIO('\n'.join(rows))
   521     return StringIO('\n'.join(rows))