[Python-Dev] Example workaround classes for using Unicode with csv module...
kent sin
kentsin at gmail.com
Thu Jun 9 10:13:31 CEST 2005
The suggestion Skip is indeed very useful, however it does not work
when some of the item is not string, here is another try:
class UnicodeReader:
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.reader = csv.reader(f, dialect=dialect, **kwds)
self.encoding = encoding
def next(self):
row = self.reader.next()
t = []
for s in row:
try:
t.append(unicode(s,self.encoding))
except:
t.append(s)
return t
# [unicode(s, self.encoding) for s in row] This will not work
with non string type
def __iter__(self):
return self
class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.writer = csv.writer(f, dialect=dialect, **kwds)
self.encoding = encoding
def writerow(self, row):
t = []
for s in row:
try:
t.append(unicode(s,"utf-8"))
except:
t.append(s)
self.writer.writerow(t)
#self.writer.writerow([s.encode("utf-8") for s in row]) #!
This is not working with non-string objects.
def writerows(self, rows):
for row in rows:
self.writerow(row)
--
Sin Hang Kin.
More information about the Python-Dev
mailing list