I want badges I want badges - 1 year ago 116
Python Question

Is there any python str() equivalent with utf-8 encoding?

I have a function that is suposed to print a variable in the screen of a python program.
Normally, I do:

mystring = str(variable)

Then I print mystring in the screen of the program with its command:


I don't know what the variable is going to be, and that's why I must do str(variable) first.
It can be a list, a dict,integer, anything.

This was working perfectly until I used some non ascii character inside myvalue.
In this case, the function shows the following unicode encode error:

UnicodeencodeError: 'ascii' codec cant decode character ...

If I use:

mystring = unicode(variable,utf-8)

Wont work because the variable can be a list, and unicode() only accepts strings. And I cant do str(variable) for obvious reasons.

Is there any function that will transform any variable using utf-8 or should I create a specific one for this purpose?
Thank you!

Answer Source

You'll have to test for the type, I am afraid:

def to_utf8_string(val):
    if not isinstance(val, basestring):
        return str(val)
    if not isinstance(val, str):
        val = val.encode('utf8')
    return val

This is pretty much what the print() command does, albeit that it detects what encoding to use from the sys.stdout stream before encoding.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download