ThanaDaray
ThanaDaray

Reputation: 1693

Encode ASCII text

I tried to encode but not success.

text = "don\\u2019t think"
textencode = text.encode('utf-8').split(" ")
print textencode

The result still ['don\u2019t', 'think']

I tried to get ['don't', 'think']

Any suggestion?

Upvotes: 2

Views: 320

Answers (2)

John La Rooy
John La Rooy

Reputation: 304463

Looks like you are using Python2. Is this what you are looking for?

>>> text = u"don\u2019t think"
>>> textencode = text.encode('utf-8').split(" ")
>>> print textencode[0]
don’t

Python3 handles the unicode objects much more nicely

>>> text = "don\u2019t think"
>>> textencode = text.split(" ")
>>> textencode
['don’t', 'think']

Upvotes: 3

shantanoo
shantanoo

Reputation: 3704

In python 2.x

>>> text = u"don\u2019t think"
>>> textencode = text.encode('utf-8').split(" ")
>>> print textencode
['don\xe2\x80\x99t', 'think']
>>> print textencode[0]
don’t

Prefix 'u' before the double quotes.

Upvotes: -1

Related Questions