Skip to content

Instantly share code, notes, and snippets.

@davisagli
Created May 17, 2016 19:15
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davisagli/93de2d80adb9cd7e8df2580eec1f03db to your computer and use it in GitHub Desktop.
Filemaker CSV reader
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