'Unicode' Error When Casting to String?


I am hoping there is a simple fix that I just don’t know of for this: If I wanted to cast some funky characters (superscripts, symbols, etc…) to a str() inside a Grasshopper widget, is there a simple way to do that properly? When I try I seem to get an error:

Runtime error (DecoderFallbackException): 
Unable to translate bytes [B0] at index 0 from specified code page to Unicode.
line 1, in script

any help is much appreciated!


Hello Ed,

Does it fix it if you add this to the first line of your file?


# coding: utf8

Thanks Graham,

When I add that line it now tells me it can’t convert from ‘Text to Complex’:

but still doesn’t seem to pass the text through?


Unicode in Python is a kinky business. Be sure to read through https://docs.python.org/2/howto/unicode.html . Not sure how much directly applies to IronPython.

Hmm … do you need to set the type hit on the output to text?

thanks so much for the suggestions! Using the .decode() I was able to get it to avoid the error at least. Adding in errors=‘replace’ can at least get around the erroring.

text_as_UTF8 = _text_In. **decode** ('utf-8', errors='replace')

But any idea how I can keep that funny character (in this case a degree symbol) as part of my output?

oh - and Graham, in mine I can only set the input type hint - not on the output - maybe cus’ I’m still on the old Rhino/GH version? I have the input (_text_In) set to ‘No Type Hint’ in the example above.

thanks again!

I think I was able to get it to work now. I used the ‘latin1’ decoding to preserve that degree symbol (°). So just in case anyone else is running into a similar issue, I got it to function with:

text_Out_ = _text_In.decode('latin1', errors='replace')