Oleg  Beat
Oleg Beat

Reputation: 337

Python unicode csv cyryllic, dont convert

I have a problem with writing cyryllic to csv. I use unicodecsv and next snippet:

import unicodecsv

ff = open('test.csv', 'wb+')
writer = unicodecsv.writer(ff, encoding='utf-8', delimiter=',', quotechar='"')
writer.writerows([[u'тест', 'aaa', 'nnn']])
ff.close()

csv generates well, but than U open it in Microsoft Excel 2011 and I see this: enter image description here

Try it in Libre office too, same problem... OS: Mac os Yosemite

don't work too with utf-8-sig:

writer = unicodecsv.writer(ff, encoding='utf-8-sig', delimiter=',', quotechar='"')

Upvotes: 0

Views: 113

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177755

Excel likes UTF-8-encoded files to have a BOM (byte order mark) signature. Use utf-8-sig as the encoding instead, else it thinks the file is ANSI-encoded. "ANSI" is locale-dependent, and is Windows-1252 on U.S. Windows.

Test source file saved with UTF-8 encoding:

#coding:utf8
import unicodecsv

with open('test.csv', 'wb+') as ff:
    writer = unicodecsv.writer(ff, encoding='utf-8-sig', delimiter=',', quotechar='"')
    writer.writerows([[u'тест', 'aaa', 'nnn']])

Output:

enter image description here

Upvotes: 1

Related Questions