Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created July 26, 2012 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdwardIII/3182183 to your computer and use it in GitHub Desktop.
Save EdwardIII/3182183 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import csv
class UnicodeCsvReader(object):
def __init__(self, f, encoding="utf-8", **kwargs):
self.csv_reader = csv.reader(f, **kwargs)
self.encoding = encoding
def __iter__(self):
return self
def next(self):
# read and split the csv row into fields
row = self.csv_reader.next()
# now decode
return [unicode(cell, self.encoding) for cell in row]
@property
def line_num(self):
return self.csv_reader.line_num
class UnicodeDictReader(csv.DictReader):
def __init__(self, f, encoding="utf-8", fieldnames=None, **kwds):
csv.DictReader.__init__(self, f, fieldnames=fieldnames, **kwds)
self.reader = UnicodeCsvReader(f, encoding=encoding, **kwds)
writer = csv.writer(open('out.csv', 'wb'))
for row in UnicodeCsvReader(open('./Products_Sample.csv', 'rb')):
if row[0]:
writer.writerow(map(lambda x: x.encode('utf8'), row))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment