Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created January 23, 2015 20:35
Show Gist options
  • Save sharoonthomas/19ed690b46f6162cc300 to your computer and use it in GitHub Desktop.
Save sharoonthomas/19ed690b46f6162cc300 to your computer and use it in GitHub Desktop.
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