Created
May 17, 2016 19:15
Star
You must be signed in to star a gist
Filemaker CSV reader
This file contains 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
import csv | |
class FilemakerCSVDictReader(): | |
"""CSV reader to read Filemaker CSV exports as dicts. | |
Applies a few transformations to all values: | |
* Decodes as cp1252 | |
* Replaces vertical tabs with newlines | |
* Splits fields containing the Group Separator control character into lists | |
""" | |
def __init__(self, f, dialect=csv.excel, encoding='cp1252', **kw): | |
self.reader = csv.DictReader(f, **kw) | |
self.encoding = encoding | |
def next(self): | |
row = self.reader.next() | |
for k, v in row.items(): | |
v = v.decode(self.encoding) | |
v = v.replace('\x0b', '\n') # vertical tab | |
if u'\x1d' in v: # group separator | |
v = v.strip().split(u'\x1d') | |
else: | |
v = v.strip() | |
row[k] = v | |
return row | |
def __iter__(self): | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment