简体   繁体   中英

Encode problems in python

I am writing a telegram bot script which shows the weather using this API call:

http://api.openweathermap.org/data/2.5/weather?q=CITY_NAME_IN_RUSSIAN&APPID=API_KEY_HERE&lang=ru&units=metric&encode=utf-8

Where the parameter CITY_NAME_IN_RUSSIAN=<city name> .

Example city name Moscow is Москва in Russian.

If I send in English, Moscow , then all is normal. But if I send in Russian, Москва , I get an error.

UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-28: ordinal not in range(128)

How can I fix it?

I assume you are using python 2. if so try unicode('Москва','utf-8') .

you can also add this on top of your file # -*- coding: utf-8 -*-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM