Damir Shakenov
Damir Shakenov

Reputation: 341

Unicode error when writing russian symbols to csv

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

Upvotes: 3

Views: 4533

Answers (4)

Hazem Elraffiee
Hazem Elraffiee

Reputation: 453

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

Upvotes: 0

Mandeep Singh
Mandeep Singh

Reputation: 185

Add this code in your file

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

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 178409

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

Upvotes: 5

ZdaR
ZdaR

Reputation: 22974

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")])

Upvotes: 0

Related Questions