algorithmic modeling for Rhino

I'm trying to save cyrillic UTF-8 string to shapefile with python plugin "pyshp".

Error is

Runtime error (EncoderFallbackException): 'ascii' codec can't decode byte 0 in position 0: ordinal not in range 

line 891, in _Writer__dbfRecords, "C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\IronPython\Lib\"
line 1005, in saveDbf, "C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\IronPython\Lib\"
line 1033, in save, "C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\IronPython\Lib\"

In Python 2.7 when "coding: UTF-8" was set everything works fine.
So, how to set default encoding in IronPython?

Pyshp plugin file in which error was

Views: 4977

Replies to This Discussion

Does calling sys.setdefaultencoding('utf-8') as stated in:

work? More background is I think here. Rhino currently uses IronPython based on Python 2.7.x.


Giulio Piacentino
for Robert McNeel & Associates

I really do not know what might be the cause of this. Maybe you might try to change the encoding in the open() call that will probably be somewhere in the library. But maybe the best idea is to ask the people who wrote the library originally. Do you have their contact at your disposal?


Giulio Piacentino
for Robert McNeel & Associates

Hi Alexey,

when you pass the cyrillic text data to the shapefile plug, is it already encoded as utf-8 or is it decoded as unicode?

If you look carefully at the source code, you can see that it assumes you are writing str data to a file, which in in python versions before python 3, is ascii. Unless you rewrite portions of, you can only pass it text data that has already been encoded in utf-8.

So depending on how you are inputting the cyrillic text, you may need to convert it to unicode, decode it to unicode, or encode it to utf-8. But when it goes to, it should be encoded as utf-8. 


These commands might be relevant

# here we get unicode type object, 
# assuming our str has been encoded as utf-8
unicode_data = data.decode("utf-8")

# here we convert the unicode to a str, encoding it using utf-8
encoded_string_data = unicode_data.encode("utf-8")






  • Add Photos
  • View All


  • Add Videos
  • View All

© 2021   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service