Skip to content

Instantly share code, notes, and snippets.

@amundo
Created March 11, 2014 05:31
Show Gist options
  • Save amundo/9479993 to your computer and use it in GitHub Desktop.
Save amundo/9479993 to your computer and use it in GitHub Desktop.
futzing with python encodings.
>>> some_ethiopic = [u"\u1234", u"\u1235", u"\u1236"]
>>> for s in some_ethiopic:
... print s, unicodedata.name(s)
...
ሴ ETHIOPIC SYLLABLE SEE
ስ ETHIOPIC SYLLABLE SE
ሶ ETHIOPIC SYLLABLE SO
>>> # ok, now we want to put these in a dictionary:
... names = {}
>>> for s in some_ethiopic: names[s] = unicodedata.name(s)
...
>>> names
{u'\u1235': 'ETHIOPIC SYLLABLE SE', u'\u1234': 'ETHIOPIC SYLLABLE SEE', u'\u1236': 'ETHIOPIC SYLLABLE SO'}
>>> import json
>>> open('/tmp/names.txt','w').write(json.dumps(names, indent=2))
>>> # i hit control-d at the next prompt
>>>
[1]+ Stopped python2.7
reveal.js/$ cat /tmp/names.txt
{
"\u1235": "ETHIOPIC SYLLABLE SE",
"\u1234": "ETHIOPIC SYLLABLE SEE",
"\u1236": "ETHIOPIC SYLLABLE SO"
}reveal.js/$ fg # this resumes python
python2.7
>>> open('/tmp/names.txt').read()
'{\n "\\u1235": "ETHIOPIC SYLLABLE SE", \n "\\u1234": "ETHIOPIC SYLLABLE SEE", \n "\\u1236": "ETHIOPIC SYLLABLE SO"\n}'
>>> # ew, everything's all escape-y and icky
>>> json.load(open('/tmp/names.txt')) # json just loads it into an object again! hooray!
{u'\u1235': u'ETHIOPIC SYLLABLE SE', u'\u1234': u'ETHIOPIC SYLLABLE SEE', u'\u1236': u'ETHIOPIC SYLLABLE SO'}
>>> names_again = json.load(open('/tmp/names.txt'))
>>> for s,v in names_again.items():
... print s, v
...
ስ ETHIOPIC SYLLABLE SE
ሴ ETHIOPIC SYLLABLE SEE
ሶ ETHIOPIC SYLLABLE SO
>>> # back in business.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment