remove Unicode from Dictionary data

11,305

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)))
Share:
11,305

Related videos on Youtube

sam
Author by

sam

Updated on July 08, 2022

Comments

  • sam
    sam almost 2 years

    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/

    • Martijn Pieters
      Martijn Pieters over 9 years
      The two examples are equal; My_Dict == Dict is True. That's because Python looks only at the contents (using the default system encoding, ASCII) to compare Unicode and byte strings.
  • trex
    trex almost 8 years
    This solution doesn't work for nested dictionaries of containing all types of datatypes. You will come across this bug with python2.7 github.com/geeknam/python-gcm/issues/61
  • Welsige
    Welsige almost 3 years
    Thanks saved my life, had a Json string and was having problems sending it to a google api thru subprocess+curl, that trick removed the Unicode and formatted the string as a dict and it got accepted by the API.