What is the python 2/3 compatible unicode () function alternative?

Is there an alternative to the python 2 function unicode()that can be used in code that will work with both python 2 and 3 and will definitely output unicode output in python 2?

  • Function unicode()does not exist in python 3
  • six.u(u'xyz') causes an error in python 2

I am writing test code where I definitely want to output unicode output so that it explodes in tests if it is combined with a non-unicode string somewhere in the execution path - for example,

'stuff %s' % u'unistuff'

In the general case, I saw that he suggested just using str(), but in python 2, which does NOT produce unicode.

I think I could do:

if six.PY3:
    unicode = str

but of course there is an official way of support.

( Google, unicode, unicode).

+4
1

Per ( ...), six.text_type:

(Unicode). unicode() Python 2 str Python 3.

+2

Source: https://habr.com/ru/post/1622224/


All Articles