Can't use unichr in Python 3.1

后端 未结 5 1030
别那么骄傲
别那么骄傲 2021-02-03 16:33

I\'ve been looking through the Python Cookbook (2nd Edition) to learn how to process strings and characters.

I wanted to try converting a number into its

相关标签:
5条回答
  • 2021-02-03 17:17

    In case you need to run in both python 2 and python 3, you can use this common syntax (the unused syntax would point to the new one)

    try:
        unichr
    except NameError:
        unichr = chr
    
    0 讨论(0)
  • 2021-02-03 17:22

    Python 3.x doesn't have a special Unicode string type/class. Every string is a Unicode string. So... I'd try chr. Should give you what unichr did pre-3.x. Can't test, sadly.

    0 讨论(0)
  • 2021-02-03 17:23

    In Python 3, you just use chr:

    >>> chr(10000)
    '✐'
    
    0 讨论(0)
  • 2021-02-03 17:27

    As suggestted by this is a better way to do it for it is compatible for both 2 and 3:

    # Python 2 and 3:
    from builtins import chr
    assert chr(8364) == '€'
    

    And you may need pip install future incase some error occures.

    0 讨论(0)
  • 2021-02-03 17:34

    In Python 3, there's no difference between unicode and normal strings anymore. Only between unicode strings and binary data. So the developers finally removed the unichr function in favor of a common chr which now does what the old unichr did. See the documentation here.

    0 讨论(0)
提交回复
热议问题