A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2005-June/054129.html below:

[Python-Dev] Example workaround classes for using Unicode with csv module...

[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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4