Z
Z
ZakirovRK2017-01-12 10:23:33
Python
ZakirovRK, 2017-01-12 10:23:33

Problems with encoding. How to decide?

Good afternoon, I ask for your help!
I am using python3.X. and I'm trying to pull out the geocode of the address using Yandex.maps.
If the address is in English, then there are no problems.

import urllib.request


def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()

yandex = 'https://geocode-maps.yandex.ru/1.x/?format=json&geocode=Moscow, Sretenka 8'
get_html(yandex).decode('utf-8')

But it's worth replacing "Moscow, Sretenka 8" with "Moscow, Sretenka 8", it gives an error related to the encoding.
I found an article https://habrahabr.ru/post/135913/ , but it did not bring clarity.
I don't understand the problem, Python3 defaults to utf-8. Why is there a problem with Russian letters?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
javedimka, 2017-01-12
@ZakirovRK

from urllib.parse import quote
from urllib import request


def get_html(url):
    response = request.urlopen(url)
    return response.read()

yandex_ru = "https://geocode-maps.yandex.ru/1.x/?format=json&geocode={0}"

get_html(yandex_ru.format(quote("Москва, Сретенка 8"))).decode("utf-8")

All laurels werevolff - Cyrillic in url Python 3, how to convert to url?
https://docs.python.org/3/library/urllib.parse.htm...

G
GavriKos, 2017-01-12
@GavriKos

1) Error in the studio
2) Methods how to solve - also desirable here
3) How do you set the Russian address? Are you writing directly to a variable? You must then specify the encoding at the beginning of the file ( stackoverflow.com/questions/3198765/how-to-write-r... ). In general - get used to the good right away - put the address in the config, for example. And the problem will be solved immediately.

S
Sly_tom_cat ., 2017-01-12
@Sly_tom_cat

and exactly you call the 3rd python?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
is this at the beginning of the file?
well, even in the second it should work fine with # -*- coding: utf-8 -*- and prefix u before the line: i.e.:
yandex = u' https://geocode-maps.yandex.ru/1.x /?format=json&ge... , Sretenka 8'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question