Skip to content

Instantly share code, notes, and snippets.

@norioxkimura
Created April 30, 2012 14:51
Show Gist options
  • Save norioxkimura/2558970 to your computer and use it in GitHub Desktop.
Save norioxkimura/2558970 to your computer and use it in GitHub Desktop.
Python 2.7: Save unicode string to file not using '\uXXXX' but using UTF-8.
# -*- coding: utf-8 -*-
import json
from codecs import open
o = { 'text': u'木村' }
with open('foo.json', 'w', encoding= 'utf-8') as fp:
json.dump(o, fp, ensure_ascii= False)
with open('foo.json', 'r', encoding= 'utf-8') as fp:
print json.load(fp)['text'].encode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment