A
A
Artem Seleznev2017-01-09 11:43:04
Python
Artem Seleznev, 2017-01-09 11:43:04

Python outputs the text correctly, but does not write to the dictionary with the wrong encoding. How to fix?

Hello.
Python2.7 (no required libraries on P3) (site from where I get this in CP1251 encoding)
The problem is that the object received from Beautiful Soup is translated into a string and written to the dictionary, but is not displayed in the correct encoding.
Example:

go = 'www.ya.ru'
regionName = newBs.find("h1",{"class":"cart"}).text.encode('utf-8')
regionName = str(regionName[14:-8]).encode('utf-8')
regionsDict[regionName]=go

At the same time, if you display a dictionary or just the regionName value, then it will be displayed correctly. Correct output:
regionsDict => Result => Yandex
regionsDict[regionName] => Result => www.ya.ru
But if you display the Key and Value, or write to a file or database, you get the following output:
'\xd0\x9c\xd0 \xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0': www.ya.ru Tried a
lot, even used this:
import sys
reload(sys)
sys.setdefaultencoding('utf-8'

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2017-01-09
@GavriKos

Конкретно с выводом пары ключ-значение - это беда питона 2.7. Выводите руками в цикле отдельно ключ, отдельно значение.
С записью в БД - скорее всего несовпадение кодировок БД и того что у вас в питоне.

A
Artem Seleznev, 2017-01-11
@seleznev_artem_info

This is what the answer was
at the beginning of the file:

#-*- coding:utf-8 -*-
import sys
reload(sys)
import locale
sys.setdefaultencoding(locale.getpreferredencoding())

And we write the variables and all the text like this:
Maybe someone will be useful.
PS The problem is poorly solved only with Cyrillic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question