# HG changeset patch # User Vincent Michel # Date 1357147022 -3600 # Node ID 1053b9d0fdf7a69a29f84a9d9c7260fa7a0b1ca4 # Parent 5096071b7e1c0184852394099cfc47d955dcd5c6 [dataimport] Allow to replace escape char in the create_copyfrom_buffer diff -r 5096071b7e1c -r 1053b9d0fdf7 dataimport.py --- a/dataimport.py Wed Sep 19 17:16:04 2012 +0200 +++ b/dataimport.py Wed Jan 02 18:17:02 2013 +0100 @@ -397,7 +397,7 @@ cnx.commit() cu.close() -def _create_copyfrom_buffer(data, columns, encoding='utf-8'): +def _create_copyfrom_buffer(data, columns, encoding='utf-8', replace_sep=None): """ Create a StringIO buffer for 'COPY FROM' command. Deals with Unicode, Int, Float, Date... @@ -420,10 +420,17 @@ value = str(value) elif isinstance(value, (str, unicode)): # Remove separators used in string formatting - if u'\t' in value or u'\r' in value or u'\n' in value: - return + for _char in (u'\t', u'\r', u'\n'): + if _char in value: + # If a replace_sep is given, replace + # the separator instead of returning None + # (and thus avoid empty buffer) + if replace_sep: + value = value.replace(_char, replace_sep) + else: + return value = value.replace('\\', r'\\') - if not value: + if value is None: return if isinstance(value, unicode): value = value.encode(encoding)