Writing CSV files with cyrillic characters using Python

I get this error:
Message: 'ascii' codec can't encode character '\u43A' in position 37: ordinal not in range(128)

Using this method - Writing out a dictionary object

# encoding:utf-8

import csv
import rhinoscriptsyntax as rs

def CSVwrite():
    #Get the filename to create
    filter = "CSV File (*.csv)|*.csv|*.txt|All Files (*.*)|*.*||"
    filename = rs.SaveFileName("Save point coordinates as", filter)
    if( filename==None ): return

    dict = [
    {'Floor':'1', 'Use':'Хол', 'Square Footage':'6598', 'RoomID':'100'},
    {'Floor':'1', 'Use':'Детска', 'Square Footage':'1900', 'RoomID':'101'},
    {'Floor':'1', 'Use':'Кухня', 'Square Footage':'1850', 'RoomID':'102'},
    {'Floor':'1', 'Use':'Баня', 'Square Footage':'250', 'RoomID':'103'},
    {'Floor':'1', 'Use':'Килер', 'Square Footage':'150', 'RoomID':'104'}
    ]


    with open(filename, "wb") as csvfile:
        fieldnames = ('Floor', 'Use', 'Square Footage', 'Price', 'RoomID', 'Capacity')
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for row in dict:
            writer.writerow(row)

##########################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    CSVwrite()

Check the last few examples on 13.1. csv — CSV File Reading and Writing — Python 2.7.18 documentation on how to handle unicode material.

Tahnks Nathan, but I used this module to solve the problem - GitHub - jdunck/python-unicodecsv: Python2’s stdlib csv module is nice, but it doesn’t support unicode. This module is a drop-in replacement which does. If you prefer python 3’s semantics but need support in py2, you probably want https://github.com/ryanhiebert/backports.csv

1 Like

That module does essentially what the documentation suggests :slight_smile: Good that you found a working solution.

But is easier for newbie like me :slightly_smiling_face: