0

I've got list that looks like this:

  [{'city': u'\u0410\u0431\u0430\u043a\u0430\u043d', 
    'language': {u'\u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439': 111,
     u'\u0418\u0442\u0430\u043b\u044c\u044f\u043d\u0441\u043a\u0438\u0439': 222, 
    u'\u0418\u0441\u043f\u0430\u043d\u0441\u043a\u0438\u0439': 333}}]

I'd like to save it as json file with utf-8 cyrillic letters. Ideally, it should look like this

  [{'city': Абакан', 
    'language': {'Английский': 111,
     'Испанский': 222, 
    'Итальянский': 333}}]

I've tried:

 #make custom json from csv
        ur=[{"city":i,
          "language":{l:n
              for l,n in zip(g['language'], g['items'])}}
           for i,g in df.groupby('city')]

 #convert to utf-8

        ur2=str(ur)
        ur2.encode('utf-8')

I also tried:

json.dumps(ur2, ensure_ascii=False).encode('utf8')

but it doesn't work. I've got:

convert_json=json.dumps(ur2, ensure_ascii=False).encode('utf8')
print(convert_json)
"[{'city': u'\\u0410\\u0431\\u0430\\u043a\\u0430\\u043d', 'language': {u'\\u0410\\u043d\\u0433\\u043b\\u0438\\u0439\\u0441\\u043a\\u0438\\u0439': 111, u'\\u0418\\u0442\\u0430\\u043b\\u044c\\u044f\\u043d\\u0441\\u043a\\u0438\\u0439': 222, u'\\u0418\\u0441\\u043f\\u0430\\u043d\\u0441\\u043a\\u0438\\u0439': 333}}]"
mailman_73
  • 778
  • 12
  • 29
  • What do you mean, "save as JSON"? Do you want a dictionary or save it to a file with the `json` module? – timgeb Mar 29 '16 at 11:39
  • @timgeb I want to save it as json file using json.dumps eventually. – mailman_73 Mar 29 '16 at 11:41
  • Also what exactly do you mean by "utf-8 letter"? utf-8 is an encoding (which you could use to encode the unicode code points in your strings) – timgeb Mar 29 '16 at 11:42
  • @timgeb the json file should contain cyrillic letters and titles like "Английский", but not "u'\u0410\u0431\u0430\u043a\u0430\u043d'" – mailman_73 Mar 29 '16 at 11:44

0 Answers0