Converting Exception to a string in Python 3

后端 未结 5 1894
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 23:08

does anyone have an idea, why this Python 3.2 code

try:    
    raise Exception(\'X\')
except Exception as e:
    print(\"Error {0}\".format(str(e)))

5条回答
  •  一整个雨季
    2021-02-04 23:57

    In Python3, string does not have such attribute as encoding. It's always unicode internally. For encoded strings, there are byte arrays:

    s = "Error {0}".format(str(e)) # string
    utf8str = s.encode("utf-8") # byte array, representing utf8-encoded text
    

提交回复
热议问题