[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnue] r8580 - trunk/gnue-common/src/utils
From: |
johannes |
Subject: |
[gnue] r8580 - trunk/gnue-common/src/utils |
Date: |
Mon, 21 Aug 2006 08:18:30 -0500 (CDT) |
Author: johannes
Date: 2006-08-21 08:18:30 -0500 (Mon, 21 Aug 2006)
New Revision: 8580
Added:
trunk/gnue-common/src/utils/ucsv.py
Log:
Adding CSV-DictWriter class which can handle unicode values
Added: trunk/gnue-common/src/utils/ucsv.py
===================================================================
--- trunk/gnue-common/src/utils/ucsv.py 2006-08-21 13:11:01 UTC (rev 8579)
+++ trunk/gnue-common/src/utils/ucsv.py 2006-08-21 13:18:30 UTC (rev 8580)
@@ -0,0 +1,110 @@
+# GNU Enterprise Common Library - Utilities - CSV writer
+#
+# Copyright 2001-2006 Free Software Foundation
+#
+# This file is part of GNU Enterprise
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# $Id$
+"""
+This module provides a csv.DictWriter class which is able to handle unicode
+values. Additionally floats will be converted using the proper decimal-point
+character according to locale.
+"""
+
+__all__ = ['UDictWriter']
+
+import csv
+import locale
+import gnue
+
+
+# =============================================================================
+# A dialect using a semicolon as delimiter
+# =============================================================================
+
+class ExcelSemicolon(csv.excel):
+ """
+ Use a semicolon as delimiter instead of the colon.
+ """
+ delimiter = ';'
+
+csv.register_dialect("ExcelSemicolon", ExcelSemicolon)
+
+# =============================================================================
+# Unicode-capabel DictWriter
+# =============================================================================
+
+class UDictWriter(csv.DictWriter):
+ """
+ Descendant of the DictWriter which is able to handle unicode values.
+ """
+
+ # -------------------------------------------------------------------------
+ # Constructor
+ # -------------------------------------------------------------------------
+
+ def __init__(self, fileobject, fieldnames, encoding="utf-8", restval="",
+ extrasaction="raise", dialect="ExcelSemicolon", *args, **kwds):
+
+ self.encoding = encoding
+ csv.DictWriter.__init__(self, fileobject, fieldnames, restval,
+ extrasaction, dialect, *args, **kwds)
+
+
+ # -------------------------------------------------------------------------
+ # Convert a row dictionary into a sequence to write
+ # -------------------------------------------------------------------------
+
+ def _dict_to_list(self, rowdict):
+ """
+ Convert the given row dictionary into a list of values to be sent to
+ the writer object.
+ """
+
+ ndict = {}
+ for (key, value) in rowdict.items():
+ if isinstance(value, unicode):
+ value = value.encode(self.encoding)
+ if isinstance(value, float):
+ decp = locale.localeconv().get('decimal_point', '.')
+ if (decp != '.'):
+ value = ("%s" % value).replace('.', decp)
+
+ ndict[key] = value
+
+ return csv.DictWriter._dict_to_list(self, ndict)
+
+# =============================================================================
+# Selftest
+# =============================================================================
+
+if __name__ == '__main__':
+
+ import datetime
+
+ data = [{'foo': u'Bl\xf6di', 'bar': None, 'sepp': 7.41, 'noo': 100},
+ {'foo': u'Bl\xf6di und so', 'bar': None, 'sepp':
+ datetime.date.today(), 'noo': 100},
+ {'foo': u'Bl\xf6di, und;so', 'bar': 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()
Property changes on: trunk/gnue-common/src/utils/ucsv.py
___________________________________________________________________
Name: svn:keywords
+ Id
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnue] r8580 - trunk/gnue-common/src/utils,
johannes <=