Answer a question

After using request library , am getting below dict in response.json()

{u'xyz': {u'key1': None, u'key2': u'Value2'}}

I want to remove all unicode characters and print only key value pairs without unicode chars

I have tried below method to remove , but it shows malformed string

>>> import json, ast
>>> c = {u'xyz': {u'key1': None,u'key2': u'Value2'}}
>>> ast.literal_eval(json.dumps(c))

Getting 'ValueError: malformed string'

Any suggestion on how to do it ?

Answers

Change your None to 'None':

 c = {u'xyz': {u'key1': 'None', u'key2': u'Value2'}}

it is a casting issue - ast likes str's

Also, maybe u want to change all None to empty str or 'None' str... See this thread : Python: most idiomatic way to convert None to empty string? with this code, i've changes the empty string to 'None':

def xstr(s):
    if s is None:
        return 'None'
    return str(s)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐