1

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 ?

2 Answers 2

2

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'))
Sign up to request clarification or add additional context in comments.

2 Comments

If someone could explain what's wrong with this answer that makes it deserve a downvote, I would love to improve it.
This line print(my_string.decode('utf_8')) gave me a direction. Thanks @TigerhawkT3
1

I run into this error frequently using SageMaker Pipelines. If you are in the same boat and are lost because the logs are not at all pointing you to what portion of code is causing AttributeError: 'tuple' object has no attribute 'decode', I can tell you that for me it always occurs because I have a dangling comma that I introduced at the end of some line of code (and my IDE did not complain about).

I've realized I induce this error whenever I am copying and pasting code or am refactoring.

For example

func(
    a=funcA(...),
    b=funcB(...),
)

gets refactored into

a=funcA(...),
b=funcB(...),
func(
    a=a,
    b=b,
)

where I have just copy pasted lines of code and kept problematic commas! Instead, remember to clean them up as

a=funcA(...)
b=funcB(...)
func(
    a=a,
    b=b,
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.