首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的python代码中Cp1252编码的问题

我的python代码中Cp1252编码的问题
EN

Stack Overflow用户
提问于 2021-04-18 18:08:26
回答 1查看 959关注 0票数 1

简短介绍:我也喜欢vim文本编辑器,python,所以我用vim编写脚本并通过git bash终端运行它们,但是两天前我遇到了一个问题。当我通过git-bash终端运行脚本时,我会遇到错误。但是,如果我通过VSCode中的集成终端运行脚本,它就能工作。

首先,我认为我使用了错误的终端编码,然后在我的终端中输入了locale,并得到了以下输出(参见附件A)。因此,当我在VSCode终端中输入命令时,得到的输出完全相同,因此没有出现问题。

“嗯,它越来越有趣了,”我想,并检查了我的vimrc配置文件,但是它很好(参见附件B)。我必须承认,我以前在编码方面没有问题,所以我想,这个问题是在语言中出现的,在API响应中使用,因为cp1252只能表示拉丁字母,而不能代表俄语!有谁有什么建议吗?我该怎么解决呢?

代码:

代码语言:javascript
运行
复制
import requests
import os


api_key = os.getenv('yandex_praktikum_api_key')


HEADERS = {
    'Authorization': f'OAuth {api_key}'
}


response = requests.get(
    'https://praktikum.yandex.ru/api/user_api/homework_statuses/',
    params={'from_date': 0},
    headers=HEADERS
)


print(response.text) # The issue happens here.

一个错误,当我使用终端:

代码语言:javascript
运行
复制
User@DESKTOP-CVQ282P MINGW64 ~/Desktop
$ python backpool.py
Traceback (most recent call last):
  File "backpool.py", line 20, in <module>
    print(response.text)
  File "D:\Python3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 63-69: character maps to <undefined>

当我使用VSCode集成终端时,正确的API响应:

代码语言:javascript
运行
复制
User@DESKTOP-CVQ282P MINGW64 ~/Desktop
$ python backpool.py
{"source":"__response__","code":"not_authenticated","message":"Учетные данные не были предоставлены."}
# As you can see, there is the Russian language

A (地区命令):

代码语言:javascript
运行
复制
User@DESKTOP-CVQ282P MINGW64 ~/Desktop
$ locale
LANG=ru_RU.UTF-8
LC_CTYPE="ru_RU.UTF-8"
LC_NUMERIC="ru_RU.UTF-8"
LC_TIME="ru_RU.UTF-8"
LC_COLLATE="ru_RU.UTF-8"
LC_MONETARY="ru_RU.UTF-8"
LC_MESSAGES="ru_RU.UTF-8"
LC_ALL=

B (全病毒):

代码语言:javascript
运行
复制
set nocompatible
filetype off

" Configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" Delete all spaces at the end of all lines (PEP8 requirement)
autocmd BufWritePre *.py normal m`:%s/\s\+$//e ``

set backspace=indent,eol,start " always enable to use backspace

set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

set showtabline=2       " always show tabline (file names)
set nu                  " show line number to the left
set ruler               " show line and column number (at the bottom)
syntax on               " syntax highlighting
color delek             " set theme of syntax
set showcmd             " show (partial) command in status line

" Set path to file finding commands (find in current dir and subdirs)
:set path=.,,**

" Encoding and backup settings
set nobackup
set noswapfile
set encoding=utf-8
set termencoding=utf-8
set fileencodings=utf-8,cp1251

" turn relative line numbers on
:set relativenumber
:set rnu

" Cursor editing: set more solid cursor
let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)

P.S.目前我可以使用VSCode运行上面的代码,但不能使用终端。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-19 11:38:08

我终于解决了。无论是python还是文本编辑器都没有问题--这是Windows 10编码的问题,所以我把它改成了UTF-8。我认为,来自Windows的勇敢的工程师肯定应该将默认编码更改为UTF ,适用于所有国家的,包括俄罗斯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67151879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档