Commits

hideki nara  committed 57435ae

Enumeration wrapper

  • Participants
  • Parent commits 6c2ddaf

Comments (0)

Files changed (1)

File csvutils/utils.py

 
     def next(self):
         return map(lambda s: force_unicode(s, encoding=self.encoding, errors=self.errors), self.reader.next())
+
+    @classmethod
+    def csv_enumerator(cls,stream, dialect=None, encoding=None, errors="strict", **kwds):
+        ''' enumerator factory for UnicodeReader
+
+            - Key must be encode in unicode.
+            
+        sample::
+
+            for index,data_dict in UnicodeReader.csv_enumerator(csv_straeam,encoding='utf8'):
+                inits = dict( [ ( k.encode('ascii') , v ) for k,v in data_dict.iteritems() ]
+                Account(**inits).save()
+        '''
+        reader = csv.DictReader(stream) 
+        reader.reader = cls(stream, dialect=None, encoding=None, errors="strict", **kwds)
+        return enumerate(reader)
+