Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Decoding and Encoding in Python

I have some text that I am trying to decode and encode in Python

import html.parser
original_tweet = "I luv my <3 iphone & you’re awsm 
 apple.DisplayIsAwesome, sooo happppppy 🙂 
 http://www.apple.com"
tweet = original_tweet.decode("utf8").encode('ascii', 'ignore')

I have entered the original tweet on one line in Spyder (Python 3.6)

I get the following message

AttributeError: 'str' object has no attribute 'decode'

Is there an alternative way to rewrite this code for Python 3.6?

Answer*

Draft saved
Draft discarded
Cancel
4
  • Thank you so much for helping me with this. That truly resolves my query. Commented Mar 10, 2018 at 10:23
  • How do I avoid losing the 're for the you're? Apologies for bugging you with this but I just noticed it. Commented Mar 10, 2018 at 10:57
  • @cordelia, the character is Unicode character U+2019 and has no direct equivalent in ASCII. What you can do, however, is to use str.replace() to replace all and with ASCII ' and the double quotation marks " and " with ASCI ". See also this question: Replacing unicode punctuation with ASCII approximations. Commented Mar 10, 2018 at 11:13
  • Thanks for this @Jens Commented Mar 10, 2018 at 11:19

lang-py

AltStyle によって変換されたページ (->オリジナル) /