我的python代码中的Cp1252编码问题[重复]

uinbv5nw  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(282)

此问题已在此处有答案

HTML from a webpage does not display foreign language characters correctly(1个答案)
两年前关闭。

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

首先,我认为我使用了错误的终端编码,然后我在终端中输入locale,并得到以下输出(参见附件A)。当我在VSCode终端中输入命令时,我得到了完全相同的输出,因此它没有问题。
“嗯,越来越有趣了”,我想,并检查了我的vimrc配置文件,但它很好(见附件B)。我需要承认,我以前没有编码问题,所以我想,问题在于API响应中使用的语言,因为cp 1252只能表示拉丁字符,但不能表示俄语!那么,有没有人有任何建议,我如何修复它?
验证码:

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.

当我使用终端时出现错误:

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>

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

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

A(locale命令):

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(完整vimrc):

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运行上面的代码,但不能使用终端。

tvmytwxo

tvmytwxo1#

我终于通过改变Windows编码解决了这个问题。

相关问题