Created
May 17, 2016 19:15
-
-
Save davisagli/93de2d80adb9cd7e8df2580eec1f03db to your computer and use it in GitHub Desktop.
Filemaker 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
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