2

I need to create a dictionary like

d1={u'apple':True}

But I have "apple" in a string say str Problem is that if I write

>>> d1={}
>>> d[u'apple']=True

It works But if I write

>>> d1={}
>>> str="apple"

Then how to insert str in the unicode u added at the beginning?

1
  • 3
    Don't confuse syntax with the value. u'...' produces an unicode object, but you don't need to use that syntax to convert a bytestring to a unicode object. You'd decode. Commented Apr 28, 2015 at 11:51

2 Answers 2

3

In Python3 u'apple' and "apple" are the same. In Python2 both behave the same.

>>> "apple"==u"apple"
True

If you need to convert between byte strings and unicode strings, use decode:

"apple".decode('utf8')
Sign up to request clarification or add additional context in comments.

2 Comments

It's not true that they behave the same in python2. They have different methods, the interface of .translate differs, etc
Also, they only behave the same to the extent they do when they happen to be pure printable ASCII, and Python only handles encodings that are supersets of printable ASCII. (And even that isn't really true; it handles Japanese encodings that swap ¥ in for backslash...)
2

You could do this:

>>> d = {}
>>> str_ = "apple"
>>> d[str_.decode('ascii')] = True
>>> d
{u'apple': True}

But my question is why bother? Since in python 2.x, you have

"apple" == u"apple"

This means that dict lookups/insertions/deletes will all work the same whether you have the unicode object or the bytes object. So it doesn't really matter.

An analogy is using a dict key with the integer 0 or the float 0., it doesn't really make any difference which you use as the key.

1 Comment

actually I am using SVM classifier from nltk. There the bag of words format have this. Thats why I am using it.

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.