Skip to content

Instantly share code, notes, and snippets.

@akkefa
Created April 10, 2018 06:44
Show Gist options
  • Save akkefa/bce01140f33a6b3878f4b8807e09585b to your computer and use it in GitHub Desktop.
Save akkefa/bce01140f33a6b3878f4b8807e09585b to your computer and use it in GitHub Desktop.
Python 3: Save unicode(Urdu) string to file not using '\uXXXX' but using UTF-8.
# -*- coding: utf-8 -*-
import json
from codecs import open
o = { 'text': 'پنجاب' }
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