Error NameError: name 'unicode' is not defined in Python

Dung Do Tien Sep 15 2021 939

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I have a small code function, I want to encode some text to iso8859_1 and print it out. Like this:

value = unicode("Python", "iso8859_1" )
lang = "Language"
str1 = unicode("name = \" "+ lang +" \"", "iso8859_1")
str2 = unicode("name = \" "+ lang +" \"value = \" "+ value +" \"", "iso8859_1")

But I get an exception NameError: name 'unicode' is not defined when I run the code above.

Traceback (most recent call last):
  File "", line 1, in <module>
    value = unicode("Python", "iso8859_1" )
NameError: name 'unicode' is not defined

And I am using python 3.8.2

Anyone can explain it to me? How can I solve it?

Thanks for any response.

Have 2 answer(s) found.
  • M

    Mahtab Sep 15 2021


    The error message "TypeError: decoding Unicode is not supported" is issued when trying to convert a string that is already there to Unicode.

    To confirm this, we can add the line "print type (details)" just before the line that triggers the error.

    Why is there a difference between the 2 almost identical Python versions? I do not know. There must be a difference in the error function calls since 'details' is one of the parameters passed to this function.

    To solve this pb: convert with a test like:

    if isinstance ( details, str ) :
       details = unicode ( details, 'latin1' )
  • C

    Chinh Nguyen Sep 15 2021

    I think version Python 3.8.2 is not supported unicode() function. You can use str() function instate of:

    value = str("Python")
    lang = "Language"
    str1 = str("name = \" "+ lang +" \"")
    str2 = str("name = \" "+ lang +" \"value = \" "+ value +" \"")


    name = " Language "
    name = " Language "value = " Python "

    I hope it useful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close