python 中的编码问题
Encode problems in python
我正在编写电报机器人 script,它使用此 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
其中参数CITY_NAME_IN_RUSSIAN=<city name>
.
示例城市名称 Moscow
在俄语中是 Москва
。
如果我用英文发送,Moscow
,那么一切正常。但是如果我用俄语发送 Москва
,我会收到错误消息。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-28: ordinal not in range(128)
我该如何解决?
我假设你正在使用 python 2. 如果是这样
试试 unicode('Москва','utf-8')
.
您也可以将其添加到文件顶部 # -*- coding: utf-8 -*-
我正在编写电报机器人 script,它使用此 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
其中参数CITY_NAME_IN_RUSSIAN=<city name>
.
示例城市名称 Moscow
在俄语中是 Москва
。
如果我用英文发送,Moscow
,那么一切正常。但是如果我用俄语发送 Москва
,我会收到错误消息。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-28: ordinal not in range(128)
我该如何解决?
我假设你正在使用 python 2. 如果是这样
试试 unicode('Москва','utf-8')
.
您也可以将其添加到文件顶部 # -*- coding: utf-8 -*-