Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert Python 2 unicode() function into correct Python 3.x syntax

Tags:

I enabled the compatibility check in my Python IDE and now I realize that the inherited Python 2.7 code has a lot of calls to unicode() which are not allowed in Python 3.x.

I looked at the docs of Python2 and found no hint how to upgrade:

I don't want to switch to Python3 now, but maybe in the future.

The code contains about 500 calls to unicode()

How to proceed?

Update

The comment of user vaultah to read the pyporting guide has received several upvotes.

My current solution is this (thanks to Peter Brittain):

from builtins import str 

... I could not find this hint in the pyporting docs.....

like image 475
guettli Avatar asked Aug 01 '16 10:08

guettli


People also ask

How do I convert Python 2 to Python 3 code?

We can convert Python2 scripts to Python3 scripts by using 2to3 module. It changes Python2 syntax to Python3 syntax. We can change all the files in a particular folder from python2 to python3.

What is Unicode in Python 3?

In Python3, the default string is called Unicode string (u string), you can understand them as human-readable characters. As explained above, you can encode them to the byte string (b string), and the byte string can be decoded back to the Unicode string.

What is Unicode in Python 2?

In python, the unicode type stores an abstract sequence of code points. Each code point represents a grapheme. By contrast, byte str stores a sequence of bytes which can then be mapped to a sequence of code points.

Is code written in Python 3 is backward compatible with Python 2?

Python 3 is not backwards compatible with Python 2, so your code may need to be adapted. Please start migrating your existing your existing Python 2 code to Python 3. Python 2 series End Of Life is set to 1st of January 2020.


2 Answers

As has already been pointed out in the comments, there is already advice on porting from 2 to 3.

Having recently had to port some of my own code from 2 to 3 and maintain compatibility for each for now, I wholeheartedly recommend using python-future, which provides a great tool to help update your code (futurize) as well as clear guidance for how to write cross-compatible code.

In your specific case, I would simply convert all calls to unicode to use str and then import str from builtins. Any IDE worth its salt these days will do that global search and replace in one operation.

Of course, that's the sort of thing futurize should catch too, if you just want to use automatic conversion (and to look for other potential issues in your code).

like image 92
Peter Brittain Avatar answered Sep 19 '22 17:09

Peter Brittain


You can test whether there is such a function as unicode() in the version of Python that you're running. If not, you can create a unicode() alias for the str() function, which does in Python 3 what unicode() did in Python 2, as all strings are unicode in Python 3.

# Python 3 compatibility hack try:     unicode('') except NameError:     unicode = str 

Note that a more complete port is probably a better idea; see the porting guide for details.

like image 28
Quint Avatar answered Sep 19 '22 17:09

Quint