Search code examples
pythonstringpython-3.xencoding

AttributeError: 'tuple' object has no attribute 'decode' while trying to decode string


I am running this code on Python 3. I encoded the data using .encode('utf_8') while accepting from the server. But now I want to decode it in order make it human readable.

 All1 = soup.findAll('tag_name', class_='class_name')
 All2 = "".join([p.text for p in All1])
 str = "1",All2.encode('utf_8')
 print(str.decode('utf_8')) 

But it is giving the following error:

print(str.decode('utf_8'))
    AttributeError: 'tuple' object has no attribute 'decode'

How can I decode it ?


Solution

  • str (don't name your variables after built-in functions, by the way) is a tuple, not a string.

    str = "1",All2.encode('utf_8')
    

    This is equivalent to the more readable:

    str = ("1", All2.encode('utf_8'))
    

    I don't know what you need the "1" for, but you might try this:

    num, my_string = '1', All2.encode('utf_8')
    

    And then decode the string:

    print(my_string.decode('utf_8'))