I want an unicoded string I am getting from a method, I want to look like original text character rather than an unicode.
a=u'\u2018\u0997\u09c7\u09ae\u09bf\u0982 \u09aa\u09cd\u09b2\u09be\u099f\u09ab\u09b0\u09cd\u09ae\u2019 \u09a4\u09c8\u09b0\u09bf \u0995\u09b0\u09ac\u09c7 \u09ab\u09c7\u09b8\u09ac\u09c1\u0995'
print a #‘গেমিং প্লাটফর্ম’ তৈরি করবে ফেসবুক
Print always works, but my use case is different. The things that it is printing, I want it put it on my RESTful API, or at least I want to use it as a string of original character and if I leave as it is my clients who will be using it on html won't be able to use it easily, I suspect.
The end result should look like this:
{title: ‘গেমিং প্লাটফর্ম’ তৈরি করবে ফেসবুক }
but json dumps is like:
json.dumps({'a': u})
'{"a": "\\\\u0996\\\\u09be\\\\u09b2\\\\u09bf\\\\u09df\\\\u09be\\\\u099c\\\\u09c1\\\\u09b0\\\\u09c0\\\\u09a4\\\\u09c7 \\\\u09a6\\\\u09c1\\\\u0987 \\\\u0997\\\\u09cd\\\\u09b0\\\\u09c1\\\\u09aa\\\\u09c7\\\\u09b0 \\\\u09b8\\\\u0982\\\\u0998\\\\u09b0\\\\u09cd\\\\u09b7\\\\u09c7 \\\\u09a8\\\\u09be\\\\u09b0\\\\u09c0\\\\u09b8\\\\u09b9 \\\\u0986\\\\u09b9\\\\u09a4 \\\\u09e7\\\\u09e6"}'
So, chances are I would need something like,
blog={}
blog['title']= str(a) # or something else
I have tried following so far, but no luck so far:
>>> str(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
>>> a.encode('utf-8')
'\xe2\x80\x98\xe0\xa6\x97\xe0\xa7\x87\xe0\xa6\xae\xe0\xa6\xbf\xe0\xa6\x82 \xe0\xa6\xaa\xe0\xa7\x8d\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\x9f\xe0\xa6\xab\xe0\xa6\xb0\xe0\xa7\x8d\xe0\xa6\xae\xe2\x80\x99 \xe0\xa6\xa4\xe0\xa7\x88\xe0\xa6\xb0\xe0\xa6\xbf \xe0\xa6\x95\xe0\xa6\xb0\xe0\xa6\xac\xe0\xa7\x87 \xe0\xa6\xab\xe0\xa7\x87\xe0\xa6\xb8\xe0\xa6\xac\xe0\xa7\x81\xe0\xa6\x95'
>>> a.encode('utf8')
'\xe2\x80\x98\xe0\xa6\x97\xe0\xa7\x87\xe0\xa6\xae\xe0\xa6\xbf\xe0\xa6\x82 \xe0\xa6\xaa\xe0\xa7\x8d\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\x9f\xe0\xa6\xab\xe0\xa6\xb0\xe0\xa7\x8d\xe0\xa6\xae\xe2\x80\x99 \xe0\xa6\xa4\xe0\xa7\x88\xe0\xa6\xb0\xe0\xa6\xbf \xe0\xa6\x95\xe0\xa6\xb0\xe0\xa6\xac\xe0\xa7\x87 \xe0\xa6\xab\xe0\xa7\x87\xe0\xa6\xb8\xe0\xa6\xac\xe0\xa7\x81\xe0\xa6\x95'
>>> a.__str__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
>>> a.decode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
repr()should convert that to'{"a": "\\u0996..."}', not to'{"a": "\\\\u0996..."}'. Probably you printed that by writinga="\u0996..."rather thana=u"\u0996...".