Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python Unicode Dict CSV Reader
# -*- coding: utf-8 -*-
"""
unicode_dict_reader
:copyright: (C) 2013-2015 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
import csv
class UnicodeDictReader(csv.DictReader):
def __init__(self, csvfile, *args, **kwargs):
"""Allows to specify an additional keyword argument encoding which
defaults to "utf-8"
"""
self.encoding = kwargs.pop('encoding', 'utf-8')
csv.DictReader.__init__(
self, csvfile, *args, **kwargs
)
def next(self):
rv = csv.DictReader.next(self)
return dict((
(k, v.decode(self.encoding).rstrip() if v else v) for k, v in
rv.iteritems()
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.