0

I want to convert python dict to Unicode as given below .

input python dictionary =

{
     "getBalance": {
        "version": "1",
        "appID": "453733",
        "customerID": "453733",
        "AccountNumber": "000190600017042"
    }
  }

expected Output in Unicode =:

"{\n      \"getBalance\": {\n         \"version\": \"1\",\n         
 \"appID\": \"453733\",\n         \"customerID\": \"453733\",\n         
  \"AccountNumber\": \"000190600017042\"\n     \t }\n   \t}\n"
3
  • 3
    what have you tried? and is this really the best format to serialize this dictionary? Commented May 15, 2019 at 6:55
  • Why don't you just linearize it? Commented May 15, 2019 at 7:01
  • BTW that's not unicode @AkshayJadhao Commented May 15, 2019 at 7:35

2 Answers 2

1

You can dump the data as a json and use it!

>>> import json
>>> dictionary = {
...      "getBalance": {
...         "version": "1",
...         "appID": "453733",
...         "customerID": "453733",
...         "AccountNumber": "000190600017042"
...     }
...   }
>>> dstr =json.dumps(dictionary)
>>> dstr
'{"getBalance": {"AccountNumber": "000190600017042", "version": "1", "customerID": "453733", "appID": "453733"}}'

And to load the data

>>> json.loads(dstr)
{u'getBalance': {u'appID': u'453733', u'version': u'1', u'customerID': u'453733', u'AccountNumber': u'000190600017042'}}
>>> type(json.loads(dstr))
<type 'dict'>

To have indents and escaping quotes, try dumping the data twice!

>>> json.dumps(dictionary)                            '{"getBalance": {"AccountNumber": "000190600017042", "version": "1", "customerID": "453733", "appID": "453733"}}'
>>> json.dumps(dictionary, indent=4)
'{\n    "getBalance": {\n        "AccountNumber": "000190600017042", \n        "version": "1", \n        "customerID": "453733", \n        "appID": "453733"\n    }\n}'
>>> print json.dumps(json.dumps(dictionary, indent=4))
"{\n    \"getBalance\": {\n        \"AccountNumber\": \"000190600017042\", \n        \"version\": \"1\", \n        \"customerID\": \"453733\", \n        \"appID\": \"453733\"\n    }\n}"
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried json.loads and json.dumps but requests.request() requires data in exacly same format as i have mentioned in expected output e.g. "{\n \"getBalance\": {\n \"version\": \"1\",\n \"appID\": \"453733\",\n \"customerID\": \"453733\",\n \"AccountNumber\": \"000190600017042\"\n \t }\n \t}\n"
If you are really concerned about the indents and escaping quotes, try dumping the data again! I've added the edits for the same
0

Given your dictionary is stored in the x variable

uni = []
for e in str(x):
    if e == '\"':
       uni.append('\\')
       uni.append(e)
       continue

    if e == '{' or e == ',':
       uni.append(e)
       uni.append('\n')
       continue

    if e == '}':
       uni.append('\t')
       uni.append(e)
       uni.append('\n')
       continue

    uni.append(e)

This could work, but as suggested in comments, why don't you just linearize the dictionary and send it as-is?

1 Comment

sending direct dict as it is not works for me i need exact data as i mentioned in expected output

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.