[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnue] r8581 - trunk/gnue-common/src/utils
From: |
johannes |
Subject: |
[gnue] r8581 - trunk/gnue-common/src/utils |
Date: |
Mon, 21 Aug 2006 08:58:29 -0500 (CDT) |
Author: johannes
Date: 2006-08-21 08:58:28 -0500 (Mon, 21 Aug 2006)
New Revision: 8581
Modified:
trunk/gnue-common/src/utils/ucsv.py
Log:
Added shorthand function for exporting to csv files; added error
handling schema for encoding
Modified: trunk/gnue-common/src/utils/ucsv.py
===================================================================
--- trunk/gnue-common/src/utils/ucsv.py 2006-08-21 13:18:30 UTC (rev 8580)
+++ trunk/gnue-common/src/utils/ucsv.py 2006-08-21 13:58:28 UTC (rev 8581)
@@ -58,12 +58,13 @@
# Constructor
# -------------------------------------------------------------------------
- def __init__(self, fileobject, fieldnames, encoding="utf-8", restval="",
- extrasaction="raise", dialect="ExcelSemicolon", *args, **kwds):
+ def __init__(self, fileobject, fieldnames, dialect="ExcelSemicolon",
+ encoding="utf-8", encode_errors="replace"):
self.encoding = encoding
- csv.DictWriter.__init__(self, fileobject, fieldnames, restval,
- extrasaction, dialect, *args, **kwds)
+ self.encode_errors = encode_errors
+ csv.DictWriter.__init__(self, fileobject, fieldnames, "", "ignore",
+ dialect)
# -------------------------------------------------------------------------
@@ -79,17 +80,48 @@
ndict = {}
for (key, value) in rowdict.items():
if isinstance(value, unicode):
- value = value.encode(self.encoding)
+ value = value.encode(self.encoding, self.encode_errors)
if isinstance(value, float):
- decp = locale.localeconv().get('decimal_point', '.')
- if (decp != '.'):
- value = ("%s" % value).replace('.', decp)
+ value = locale.str(value)
ndict[key] = value
return csv.DictWriter._dict_to_list(self, ndict)
+
# =============================================================================
+# Shorthand for creating a CSV file from a dictionary
+# =============================================================================
+
+def write_file(fieldnames, data, filename, dialect="ExcelSemicolon",
+ encoding="utf-8", encode_errors="replace", caption=None):
+ """
+ Create a CSV file using the given fieldnames and data
+ @param fieldnames: list of fields to export
+ @param data: list of dictionaries with the data to export
+ @param filename: name of the file to export the data to
+ @param dialect: name of a registered dialect (as used by the csv module)
+ @param encoding: an encoding used for unicode values
+ @param encode_errors: error handling schema for encoding
+ @param caption: dictionary giving a nice caption per column. If no such
+ mapping is provided the fieldnames will be used as header line
+ """
+ fhd = open(filename, 'wb')
+ try:
+ writer = UDictWriter(fhd, fieldnames, dialect, encoding, encode_errors)
+
+ if not caption:
+ caption = {}
+ for field in fieldnames:
+ caption[field] = field
+ writer.writerow(caption)
+ writer.writerows(data)
+
+ finally:
+ fhd.close()
+
+
+# =============================================================================
# Selftest
# =============================================================================
@@ -97,14 +129,12 @@
import datetime
- data = [{'foo': u'Bl\xf6di', 'bar': None, 'sepp': 7.41, 'noo': 100},
- {'foo': u'Bl\xf6di und so', 'bar': None, 'sepp':
+ xk = u'bar\xf6xy'
+ test = [{'foo': u'Bl\xf6di', xk: None, 'sepp': 7.41, 'noo': 100},
+ {'foo': u'Bl\xf6di und so', xk: None, 'sepp':
datetime.date.today(), 'noo': 100},
- {'foo': u'Bl\xf6di, und;so', 'bar': None, 'sepp':
+ {'foo': u'Bl\xf6di, und;so', xk: None, 'sepp':
datetime.date.today(), 'noo': 100},
]
- out = open('out.csv', 'w')
- xcs = UDictWriter(out, ['foo', 'sepp', 'bar', 'noo'], 'cp1252')
- xcs.writerows(data)
- out.close()
+ write_file(test[0].keys(), test, 'out.csv', encoding="cp1250")
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnue] r8581 - trunk/gnue-common/src/utils,
johannes <=