Created
July 26, 2012 13:57
-
-
Save EdwardIII/3182183 to your computer and use it in GitHub Desktop.
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
#!/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