Created
January 23, 2015 20:35
-
-
Save sharoonthomas/19ed690b46f6162cc300 to your computer and use it in GitHub Desktop.
Python Unicode Dict CSV Reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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