简体   繁体   English

python中的编码问题

[英]Encode problems in python

I am writing a telegram bot script which shows the weather using this API call:我正在编写一个电报机器人脚本,它使用这个 API 调用显示天气:

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> .其中参数CITY_NAME_IN_RUSSIAN=<city name>

Example city name Moscow is Москва in Russian.示例城市名称Moscow是俄语的Москва

If I send in English, Moscow , then all is normal.如果我用英语, Moscow发送,那么一切都很正常。 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') .我假设您使用的是 python 2. 如果是这样,请尝试unicode('Москва','utf-8')

you can also add this on top of your file # -*- coding: utf-8 -*-您也可以将其添加到文件顶部# -*- coding: utf-8 -*-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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