user3812142
user3812142

Reputation: 87

UnicodeEncodeError while writing to CSV

When trying to write the data in @columns using the code below I get the following error: "UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 2: ordinal not in range(128)"

I've tried running encode/decode to ascii but ...

u'G\xe5ng'.encode('ascii')

... yields the same error message. Any ideas on how to solve this?

    writer = csv.writer(out_file, delimiter=';',
                            quotechar='"', quoting=csv.QUOTE_ALL)

    columns = ['Gods', u'G\xe5ng', 'Cykel', 'Buss', 'Bil', u'F\xe4rja', u'Sj\xf6fart', u'T\xe5g/sp\xe5rv\xe4g']


    writer.writerow(columns)

Upvotes: 1

Views: 1294

Answers (1)

Neel
Neel

Reputation: 21243

You have to use UnicodeWriter give in python doc

class UnicodeWriter:
    """
    A CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
        # Redirect output to a queue
        self.queue = cStringIO.StringIO()
        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
        self.stream = f
        self.encoder = codecs.getincrementalencoder(encoding)()

    def writerow(self, row):
        self.writer.writerow([s.encode("utf-8") for s in row])
        # Fetch UTF-8 output from the queue ...
        data = self.queue.getvalue()
        data = data.decode("utf-8")
        # ... and reencode it into the target encoding
        data = self.encoder.encode(data)
        # write to the target stream
        self.stream.write(data)
        # empty queue
        self.queue.truncate(0)

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

You will get full description on CSV Python Example

Upvotes: 1

Related Questions