python - Unicode strings returned by API not equal to my dict -
so i'm trying compare dict have created dict response returned boto3 call.
the response representation of json document , want check same.
boto3 returned strings unicode. here's response:
{u'version': u'2012-10-17', u'statement': [{u'action': u'sts:assumerole', u'principal': {u'service': u'ec2.amazonaws.com'}, u'effect': u'allow', u'sid': u''}]}
i created dict this:
default_documment = {} default_documment['version'] = '2012-10-17' default_documment['statement'] = [{}] default_documment['statement'][0]['sid'] = '' default_documment['statement'][0]['effect'] = 'allow' default_documment['statement'][0]['principal'] = {} default_documment['statement'][0]['principal']['service'] = 'ec2.amazonaws.com' default_documment['statement'][0]['action'] = 'sts:assumerole'
however, when compare these 2 dicts == not equal.
so tried adding u strings when create dict:
# default document new role default_documment = {} default_documment[u'version'] = u'2012-10-17' default_documment[u'statement'] = [{}] default_documment[u'statement'][0][u'sid'] = u'' default_documment[u'statement'][0][u'effect'] = u'allow' default_documment[u'statement'][0][u'principal'] = {} default_documment[u'statement'][0][u'principal'][u'service'] = u'ec2.amazonaws.com' default_documment[u'statement'][0][u'action'] = u'sts:assumerole'
this doesn't work either. dicts not equally , if print of dict doesn't show u'somestring' shows 'somestring'.
how can compare dict boto3 has returned?
your second attempt works correctly in python 2.7 , 3.3. below cut-and-paste of boto3 response , code (with document spelling corrected :)
d = {u'version': u'2012-10-17', u'statement': [{u'action': u'sts:assumerole', u'principal': {u'service': u'ec2.amazonaws.com'}, u'effect': u'allow', u'sid': u''}]} default_document = {} default_document[u'version'] = u'2012-10-17' default_document[u'statement'] = [{}] default_document[u'statement'][0][u'sid'] = u'' default_document[u'statement'][0][u'effect'] = u'allow' default_document[u'statement'][0][u'principal'] = {} default_document[u'statement'][0][u'principal'][u'service'] = u'ec2.amazonaws.com' default_document[u'statement'][0][u'action'] = u'sts:assumerole' print(d == default_document)
output:
true
Comments
Post a Comment