Python how to save dictionary with cyrillic symbols into json file
Question: 
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import json
    
    
    d = {'a':'текст',
         'b':{
             'a':'текст2',
             'b':'текст3'
         }}
    print(d)
    
    w = open('log', 'w')
    json.dump(d,w, ensure_ascii=False)
    w.close()
    

It gives me: UnicodeEncodeError: 'ascii' codec can't encode characters in
position 1-5: ordinal not in range(128)


Answer: Post the full traceback, the error could be coming from the print statement
when it fails to decode the dictionary object. For some reason print statement
cannot decode all contents if you have Cyrillic text in it.

Here is how I save to json my dictionary that contains Cyrillics:

    
    
    mydictionary = {'a':'текст'}
    filename = "myoutfile"
    with open(filename, 'w') as jsonfile:
         json.dump(mydictionary, jsonfile, ensure_ascii=False)
    

The trick will be reading in json back into dictionary and doing things with
it.

To read in json back into dictionary:

    
    
    with open(filename, 'r') as jsonfile:    
    newdictonary = json.load(jsonfile)
    

Now when you look at the dictionary, the word 'текст' looks (encoded) like
'\u0442\u0435\u043a\u0441\u0442'. You simply need to decode it using
encode('utf-8'):

    
    
    for key, value in newdictionary.iteritems():
           print value.encode('utf-8')
    

Same goes for lists if your Cyrillic text is stored there:

    
    
    for f in value:
        print f.encode('utf-8')
        # or if you plan to use the val somewhere else:
        f = f.encode('utf-8')
    

