1
votes

I want to write cyrillic symbols to csv file but I get unicode encode error. English symbols works perfect. I'm using Python 3.6.2.

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-6: ordinal not in range(128)

import csv


with open("test.csv", 'w') as csvfile:
    csvfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    hello = 'привет, мир!'
    csvfile.writerow([hello])
4
it looks like you are running script with python2, in py3 unicode is a default string representation which means you should be fine writing data to the csv file. - Taras Matsyk
@TarasMatsyk my PyCharm is configured for python 3.6.2 interpretor - Damir Shakenov

4 Answers

4
votes

Declare the encoding of the file when you open it. newline='' is also required per the csv documentation.

import csv

with open('test.csv','w',encoding='utf8',newline='') as csvfile:
    csvfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    hello = 'привет, мир!'
    csvfile.writerow([hello])
0
votes

You just need to encode the hello string before you write it to a file (csv). Otherwise Python is expecting you to input only ascii characters, in case of non-ascii characters, you may use utf-8 encoding as:

# -*- coding: utf-8 -*-
import csv


with open("test.csv", 'w') as csvfile:
    csvfile = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    hello = u'привет, мир!' # Better way of declaring a unicode string literal
    csvfile.writerow([hello.encode("utf-8")])
0
votes

Add this code in your file

   # encoding=utf8  
   --------------------------------------
   import sys  
   reload(sys)  
   sys.setdefaultencoding('utf8')  
0
votes

For Python 2 guys, use this instead of the normal "open" function:

import codecs
codecs.open(out_path, encoding='utf-8', mode='w')

This is equivalent of the following in Python 3:

open(out_path, 'w', encoding='utf8')