I have following dictionary
My_Dict= { u'Apple': [u'A' , u'B'] ,u'orange': [u'C' , u'D'] }
I have another dictionary with same data but not in Unicode
Dict= { 'Apple': ['A' , 'B'] ,'orange': ['C' , 'D'] }
I am trying to compare both dictionary but its saying both dictionary are not same. I assume its because of that Unicode
Is there any way I can compare both dictionary by removing Unicode from My_dict or converting 'Dict' to Unicode ?
The reason I got Unicode is because I am using S-expression parser. Link to parser module is below for reference. http://sexpdata.readthedocs.org/en/latest/
To remove Unicode from Dictionary Try:
import json, ast
My_Dict= { u'Apple': [u'A' , u'B'] ,u'orange': [u'C' , u'D'] }
print(ast.literal_eval(json.dumps(My_Dict)))
import json
some_other_dict = json.loads(json.dumps(My_Dict))
print(some_other_dict)
Try this one. It will remove unicode.
Maybe perform a json.dumps on both.
json.dumps({ u'Apple': [u'A' , u'B'] ,u'orange': [u'C' , u'D'] })
Output: `'{"orange": ["C", "D"], "Apple": ["A", "B"]}'
json.dumps({ 'Apple': ['A' , 'B'] ,'orange': ['C' , 'D'] })
Output: `'{"orange": ["C", "D"], "Apple": ["A", "B"]}'`
For removing unicode you have to convert key and value from unicode.
Check the below code
My_Dict= { u'Apple': [u'A' , u'B'] ,u'orange': [u'A' , u'B'] }
Dict= { 'Apple': ['A' , 'B'] ,'orange': ['C' , 'D'] }
My_Dicts={}
for i, v in My_Dict.items():
ky= i.encode('ascii','ignore')
if type(v)is list:My_Dicts[ky]=[ item.encode('ascii') for item in v ]
else: My_Dicts[ky] =v.encode('ascii','ignore')
My_Dict=My_Dicts
print My_Dict
print Dict
Output
{'orange': ['C', 'D'], 'Apple': ['A', 'B']}
{'orange': ['C', 'D'], 'Apple': ['A', 'B']}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.