Converting Exception to a string in Python 3

后端 未结 5 1892
没有蜡笔的小新
没有蜡笔的小新 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:49

    In Python 3, you are already in "unicode space" and don't need encoding. Depending on what you want to achieve, you should the conversion do immediately before doing stuff.

    E.g. you can convert all this to bytes(), but rather in the direction

    bytes("Error {0}".format(str(e)), encoding='utf-8')
    

    .

提交回复
热议问题