Assign one dictionary value to another dictionary value in python -
this question has answer here:
i have 1 question assigning 1 dictionary value dictionary value in python, value contains chinese characters
# -*- coding: utf-8 -*- import string = {} a['1'] = '大' # chinese character b = {} b['1'] = a['1'] print a['1'] print print b
and printout
大 {'1': '\xe5\xa4\xa7'} {'1': '\xe5\xa4\xa7'}
why there difference between a
, a['1']
? how make print a
{'1': '大'}
?
why there difference between , a['1']?
the first (a
) character wrapped in dictionary. when print dictionary, python print raw bytes of character (\xe5\xa4\xa7
) it's utf-8 encoding. when print string directly using print a[1]
, python decode these 3 bytes respective character.
how make a['1'] , b['1'] 大?
they same. do
print a['1'] print b['1']
add following class dump dictionary expect. note this code assumes strings in utf-8 format.
class myprettyprinter(pprint.prettyprinter): def format(self, object, context, maxlevels, level): if isinstance(object, str): return (object.decode('utf8'), true, false) return pprint.prettyprinter.format(self, object, context, maxlevels, level) myprettyprinter().pprint(a) # {1: 大}
Comments
Post a Comment