import unicodecsv as csv
def isstring(_str):
return isinstance(_str, str) or isinstance(_str, unicode)
def read_csv(filename):
list_of_dicts = []
with open(filename, 'rb') as f:
reader = csv.DictReader(f)
list_of_dicts = [row for row in reader]
return list_of_dicts
def save_csv(list_of_dicts, filename):
fieldnames = set([k for _dict in list_of_dicts for k in _dict])
with open(filename, 'wb') as f:
writer = csv.DictWriter(f, fieldnames)
writer.writeheader()
for _dict in list_of_dicts:
_d = {k: v.encode("utf-8", errors="ignore") if isstring(v) else v for k, v in _dict.iteritems()}
writer.writerow(_d)