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

guettli

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.....

Peter Brittain

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

From Dev

Regex unicode in python 2.x vs 3.x

From Dev

The statement d = ('x': 1, 'y': 2, 'z': 3) is showing errors in Python 3.6. What would be the correct syntax for the new version ?

From Dev

How to convert a string with unicode in it to unicode using python

From Dev

python3: How to convert a string with Unicode encoding to a string of letters

From Dev

How to convert symbols into their respective unicode representation using python3?

From Dev

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

From Dev

Python 3.x and printing Unicode symbols

From Dev

HTML Python Correct Syntax

From Dev

Python SyntaxError with a correct syntax

From Dev

Convert .py files to correct encoding for Python 3

From Dev

How to convert unicode to its original character in Python

From Dev

How to convert list of bytes (unicode) to Python string?

From Dev

How to convert unicode to integer or float in dictionary in Python

From Dev

How to convert unicode to integer or float in dictionary in Python

From Dev

How to convert unicode to its original character in Python

From Dev

How do filter and convert unicode to integer in python

From Dev

how to convert list of unicode url to string in python

From Dev

Troubleshooting Unicode string behavior when updating Python 2.x->3.x

From Dev

How to convert from a unicode code point to an actual unicode string in python?

From Dev

How to convert from a unicode code point to an actual unicode string in python?

From Dev

Convert str to unicode in python

From Dev

What is the correct syntax for launching ipython qtconsole/notebook for python version=x

From Dev

What is the correct syntax for launching ipython qtconsole/notebook for python version=x

From Dev

the meaning of python3.x syntax

From Dev

How to convert sort using cmp from python 2 to python 3?

From Dev

encoding unicode in dictionary not correct [python]

From Dev

Valid syntax in both Python 2.x and 3.x for raising exception?

Related Related

  1. 1

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

  2. 2

    How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

  3. 3

    Regex unicode in python 2.x vs 3.x

  4. 4

    The statement d = ('x': 1, 'y': 2, 'z': 3) is showing errors in Python 3.6. What would be the correct syntax for the new version ?

  5. 5

    How to convert a string with unicode in it to unicode using python

  6. 6

    python3: How to convert a string with Unicode encoding to a string of letters

  7. 7

    How to convert symbols into their respective unicode representation using python3?

  8. 8

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

  9. 9

    Python 3.x and printing Unicode symbols

  10. 10

    HTML Python Correct Syntax

  11. 11

    Python SyntaxError with a correct syntax

  12. 12

    Convert .py files to correct encoding for Python 3

  13. 13

    How to convert unicode to its original character in Python

  14. 14

    How to convert list of bytes (unicode) to Python string?

  15. 15

    How to convert unicode to integer or float in dictionary in Python

  16. 16

    How to convert unicode to integer or float in dictionary in Python

  17. 17

    How to convert unicode to its original character in Python

  18. 18

    How do filter and convert unicode to integer in python

  19. 19

    how to convert list of unicode url to string in python

  20. 20

    Troubleshooting Unicode string behavior when updating Python 2.x->3.x

  21. 21

    How to convert from a unicode code point to an actual unicode string in python?

  22. 22

    How to convert from a unicode code point to an actual unicode string in python?

  23. 23

    Convert str to unicode in python

  24. 24

    What is the correct syntax for launching ipython qtconsole/notebook for python version=x

  25. 25

    What is the correct syntax for launching ipython qtconsole/notebook for python version=x

  26. 26

    the meaning of python3.x syntax

  27. 27

    How to convert sort using cmp from python 2 to python 3?

  28. 28

    encoding unicode in dictionary not correct [python]

  29. 29

    Valid syntax in both Python 2.x and 3.x for raising exception?

HotTag

Archive