1

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) 
0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.