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.
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com