user1447941
user1447941

Reputation: 3915

How can I remove the Unicode u from a JSON item?

>>> stuff = '[["hiya"]]'
>>> js = json.loads(stuff)
>>> js[0]
[u'hiya']
>>> str(js[0])
"[u'hiya']"

It doesn't seem to go away. How can I print hiya on its own (without manually stripping the special characters away)?

Upvotes: 2

Views: 4754

Answers (2)

Heroic
Heroic

Reputation: 980

Another solution is use maping and join to convert a list to string. but i not recommend you to use this but you can use when you need directly convert list to a string. othrewise above solution is fine.

for eg.

import json

stuff = '[["hiya"]]'

js = json.loads(stuff)

print ''.join(map(str,js[0]))

Upvotes: 0

Rob Wouters
Rob Wouters

Reputation: 16327

You have a list that's nested two levels deep. Try it like this to simply print 'hiya':

>>> import json
>>> stuff = '[["hiya"]]'
>>> js = json.loads(stuff)
>>> str(js[0][0])
'hiya'

Upvotes: 8

Related Questions