N
N
Nikolay Baranenko2017-07-26 21:54:13
Python
Nikolay Baranenko, 2017-07-26 21:54:13

How to solve encoding (cyrillic) problem when using SIMPLEJSON?

Hello.
I'm trying to work with JSON whose content has Cyrillic
code

#!/usr/bin/python
import simplejson as json
import os

os.environ["NLS_LANG"] = ".UTF8"

part_nums = ['RUN ВАСЯ RUN','CL05B103KB5NNNC','CC0402KRX5R8BB104']

# print(json.loads('Вася беги'.decode('utf8')))

def json_list(list):
    lst = []
    for pn in list:
        d = {}
        d['mpn']=pn
        lst.append(d)
    return json.dumps(lst)

print(json_list(part_nums))

print (json_list(part_nums).encode('UTF8').decode('UTF8'))

as a result
[{"mpn": "RUN \u0412\u0410\u0421\u042f RUN"}, {"mpn": "CL05B103KB5NNNC"}, {"mpn": "CC0402KRX5R8BB104"}]

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-07-26
@drno-reg

json.dumps(lst, ensure_ascii=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question