A
A
Anton Biryukov2018-07-07 10:07:46
Python
Anton Biryukov, 2018-07-07 10:07:46

Problems with encoding when converting from xls to json?

Проблема заключается в следующем при конвертации в json файл записывается следующий результат
[{"kat-id": "58:08:0500111:86", "vid_iz": "\u0414\u043b\u044f \u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0441\u043e\u0431\u043d\u043e\u0433\u043e \u0445\u043e\u0437\u044f\u0439\u0441\u0442\u0432\u0430", "pl": 1300.0, "adres": "\u043e\u0431\u043b.
Взял частично из файла , но думаю суть понятно. То есть кириллицу он записывается в таком формате \u043b
Исходники:

# -*- coding: utf-8 -*-
import xlrd
from collections import OrderedDict
import simplejson as json
# Open the workbook and select the first worksheet
wb = xlrd.open_workbook('4.xlsx', encoding_override='utf-8')
sh = wb.sheet_by_index(0)
# List to hold dictionaries
cars_list = []
# Iterate through each row in worksheet and fetch values into dict
for rownum in range(1, sh.nrows):
    kadastr = OrderedDict()
    row_values = sh.row_values(rownum)
    kadastr['kat-id'] = row_values[0]
    kadastr['vid_iz'] = row_values[1]
    kadastr['pl'] = row_values[2]
    kadastr['adres'] = row_values[3]
    kadastr['name_is'] = row_values[4]
    kadastr['pr_rez'] = row_values[5]
    cars_list.append(kadastr)
# Serialize the list of dicts to JSON
j = json.dumps(cars_list)
print(cars_list)
# Write to file
with open('data.json', 'w', encoding='cp1251') as f:
    f.write(j)

Пробовал и явно прописать кодировку и нет все равно так выводит при чем при печати print(cars_list) выводит нормально

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Антон Бирюков, 2018-07-07
@iiantonii

Вопрос решил сам. Вот исходник

# -*- coding: utf-8 -*-
import xlrd
from collections import OrderedDict
import simplejson as json
import codecs
# Open the workbook and select the first worksheet
wb = xlrd.open_workbook('4.xlsx', encoding_override='utf-8')
sh = wb.sheet_by_index(0)
# List to hold dictionaries
cars_list = []
# Iterate through each row in worksheet and fetch values into dict
for rownum in range(1, sh.nrows):
    kadastr = OrderedDict()
    row_values = sh.row_values(rownum)
    kadastr['kat-id'] = row_values[0]
    kadastr['vid_iz'] = row_values[1]
    kadastr['pl'] = row_values[2]
    kadastr['adres'] = row_values[3]
    kadastr['name_is'] = row_values[4]
    kadastr['pr_rez'] = row_values[5]
    cars_list.append(kadastr)
# Serialize the list of dicts to JSON
# j = json.dumps(cars_list, ensure_ascii=False).encode('utf8')
# j = json.dumps(cars_list, encoding='utf-8')
print(cars_list)

# Write to file


with codecs.open('data.json', 'w', encoding='utf-8') as f:
    json.dump(cars_list, f, ensure_ascii=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question