--- 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)