python - Unexpected output when saving/loading dictionary to/from json -
python 2.7.6 on windows 7 code use:
import json settings = { "vo" : "direct3d", "ao" : "dsound", "volume" : "100", "priority" : "abovenormal"} json.dump(settings, open('settings.json', 'w')) settings = json.load(open('settings.json', 'r')) print settings
in settings.json get:
{"volume": "100", "priority": "abovenormal", "ao": "dsound", "vo": "direct3d"}
at end console outputs:
{u'volume': u'100', u'priority': u'abovenormal', u'ao': u'dsound', u'vo': u'direct3d'}
what doing wrong?
you doing nothing wrong; that's exact output should expecting.
json deals exclusively unicode strings; u''
strings such unicode values. if strings contain characters within ascii range, you'll not notice difference when handling these.
in other words, using mixing regular (byte) strings these values work, python transparently encode , decode between 2 types. you'll notice problems when text contains characters fall outside of ascii range. think accented characters, or symbols, or asian scripts, example. in case should learn how use unicode properly, see python unicode howto.
Comments
Post a Comment