The problem with Cp1252 encoding in my python code [duplicate] - python

This question already has an answer here:
HTML from a webpage does not display foreign language characters correctly
(1 answer)
Closed 1 year ago.
Short Introduction: I like vim text editor, python as well, so I write scripts in vim and run them through git-bash terminal, but two days ago I face a problem. When I run script through git-bash terminal, I am getting error. However, If I run the script through integrated terminal in VSCode, it works.
First of all, I thought, that I had used wrong terminal encoding, then I typed locale in my terminal, and got the following output (see attachment A). The exact same output I got, when I typed command in VSCode terminal, therefore an issue was not in it.
"Well, it is getting interesting", I thought, and checked my vimrc config file, but it was fine (see attachment B). I need to admit, that I had not problem with encoding before, so I suppose, that the issue is in the language, that is used in API response, because cp1252 can represent only Latin characters, but not Russian! So, does anyone has any suggestions how can I fix it?
Code:
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.
An error, when I use terminal:
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>
Correct API response, when I use VSCode integrated terminal:
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 command):
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 (full 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. Currently I can run the code above using VSCode, but cannot using the terminal.

I have resolved it finally. There was not the problem neither in python, nor in text editors -- it was in Windows 10 encoding, so I changed it to UTF-8. I think, that brave engineers from Windows should definitely change default encoding to UTF for all countries, including Russia.

Related

introduce <Tab> in new line while creating a block for python files in vim

I want to introduce a tab character in the new line for creating a new block in vim for python files.
Example
when I write
if(a < 5):
and press Enter, the cursor should come in next line with a tab after if in vertical alignment from the above line. Something like this
if(a < 5):
a = 5
I have configured my .vimrc file like this
set nu
set autoindent
set tabstop=4
syntax enable
set showmatch
colorscheme gruvbox
set bg=dark
"automatically creates a block after :
autocmd FileType python inoremap :<CR> :<CR><Tab>
Note:
1) Last line, I have written for the intended purpose mentioned in my question.
2) autoindent is set.
3) I dont want to use any plugin.
Problem I am facing:
When I press Enter after : , in addition to tab, one extra space is also created which creates wrong indentation.
Thanks in advance for helping
The way you're trying to do this is the hard way. There's a filetype plugin and a Python syntax type and it already knows what to do.
filetype plugin indent on
And don't use set autoindent. It's not very intelligent, it just copies the indent from the previous line. It's the opposite of what you're trying to do.
Files ending in .py should automatically get the right settings. If you need to force the current syntax type in a buffer, you can do that with
:set filetype=python
If you want to use tabs instead of spaces,
" Globally
set noexpandtab
" Python only
autocmd FileType python set noexpandtab
However, I'll note that using tabs in Python is not recommended and goes against published best practices (see PEP 8).
Lastly I'll mention that "plugin" is just a Vim term. All of this ships with Vim by default, this does not create a third-party dependency.

End of script output before headers

When I create the file test.py (see code below) in an editor on Windows (I tried Netbeans, PyCharm and Notepad++) and upload it to the server (Ubuntu) I receive this error:
End of script output before headers: test.py
But when I create the file directly on the server using vi command line editor the page is displayed without any error. Any idea how to fix this issue ?
Here's the code for test.py
#!/usr/bin/python
# send content type
print("Content-Type: text/html\n\n")
print("Good")
I think that's because of windows carriage return characters.
These are two characters:
\r is carriage return;
\n is line feed.
Two characters combined represent a new line on Windows. Whereas on Linux, \n represents new line.
Notepad++ has an option to specify which format you want to use:
Go to Settings -- > Preferences and the choose linux:

vim expandtab doesn't work after new installation

let mapleader = ","
set number
set textwidth=79 " lines longer than 79 columns will be broken
set shiftwidth=4 " operation >> indents 4 columns; << unindents 4 columns
set tabstop=4 " a hard TAB displays as 4 columns
set expandtab " insert spaces when hitting TABs
set softtabstop=4 " insert/delete 4 spaces when hitting a TAB/BACKSPACE
set shiftround " round indent to multiple of 'shiftwidth'
set cindent " align the new line indent with the previous line
set nobackup
set nowritebackup
set noswapfile
vnoremap < <gv " continue visual selecting after shiftwidh
vnoremap > >gv
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
nnoremap j gj
nnoremap k gk
nnoremap <Leader>r :w \| !clear && ./%<CR>
command W w !sudo tee % > /dev/null
noremap <silent><Leader>/ :nohls<CR>
set clipboard=unnamedplus
set paste
set ignorecase
Somehow after reinstallation of my arch linux, vim stoped work poperly.
After doing the same I did couple days ago with old system - now python complains about indentation.
I have no installed plugins or whatever, why does this broke?
P.S. already viewed same quiestions, but them was about plugins, which I doesn't have.
P.S. Noticed that after : vim won't start newline according cindent
Still indentation brokes after :set paste. Why this happens?
set paste "breaks" the indentation. That is all it does. That is the reason it works. Pasting text into Vim would normally be the same thing as just typing each letter. For example, open a file that has some text in it (in Vim), and make sure that Vim is in normal mode. Copy the following text: d4dAhowdy. Paste it into Vim. You will see that it deletes four lines (d4d), changes to insert mode at the end of the line (A), and types howdy. Pasting into Vim is the same thing as typing the letters; it does not necessarily just paste everything exactly how it comes. Let's say you type:
if this:
that
Once you hit Enter after if this:, Vim would indent the line, so that code would actually appear as:
if this:
that
Using set paste turns that off so that when you paste that code in (with the indentation included), Vim does not auto-indent and everything shows up normally. If you were to set nopaste and then paste the above unindented code, Vim would indent it for you.

Spyder IDE: How do you configure default end-of-line character?

I am using Spyder IDE to develop code and the IDE currently has default end-of-line character set to be CRLF. I would like to use '\n' instead because all my existing source code uses '\n' and so I don't want to have a bunch of new files that use a different end-of-line character.
Is there a way to specify the default end-of-line character in Spyder IDE? If yes, how?
(Spyder maintainer here) To configure the end of line characters you want to use, you need to go to the menu
Tools > Preferences > Editor > Advanced settings
then to the section End of line characters and select the ones that Spyder will use on save.
I just started exploring Spyder 4.1.5 (unfortunately could not get 5.1.5 installed), and found this for EOL settings:
'Tools' > 'Preferences' > choose 'Editor' in the list on the left > 'Advanced setting' tab on the right > at the bottom is the 'End-of-line characters' section.
To convert existing CRLF to LF, select the LF option in the dropdown, then for each file you want to convert, make any edit and save the file and voila. (I think you have to do the edit+save step file-by-file.)
As Carlos Cordoba wrote, Spyder does not allow setting the line ending but it does respect the line endings of a file.
If a file has LF line endings and you hit enter, it wont add CRLF even if you are on windows.
My solution is to change the line endings of a new file with notepad++ (edit-> EOL conversion)
For many existing files you can use the replace function, explained here

macVim: line continuation in python creates unnecessary tab?

Whenever I want to use line continuation in my python script using macVim, I receive the following format:
a = one,\
two,\
three
instead of:
a = one,\
two,\
three
I've rifled through help, and was unable to locate what exactly is causing macVim to append an extra tab upon using line continuation.
Note: I currently have the following in my .vimrc file:
set tabstop=4
set shiftwidth=4
set expandtab
set softtabstop=4
You have your tabs set to 4 and not 2
Make sure that you have
filetype plugin indent on
You can also use something as followed in your .vimrc as a oneliner for defaults instead of adding them on separate lines
Use 2 instead of 4
set ts=2 sts=2 sw=2 noexpandtab
Your settings are almost fine. In Python code, the tab settings should always be
:set tabstop=8 shiftwidth=4 softtabstop=4 expandtab
These settings honour the recommendations in the "Style guide for Python code" known as PEP 8 and are universally followed in Python code. Stick to these settings, or even better, set up an autocommand that sets them automatically. In your vimrc:
autocmd FileType python setlocal ts=8 sw=4 sts=4 et
Now on to your problem: This is caused by the default indentation rules in Vim's indent script for Python. Fortunately, these rules can be customised. In your case you just need to put the following line in your vimrc.
let g:pyindent_continue = '&shiftwidth'
(In newer versions of Vim, use 'shiftwidth()' instead of '&shiftwidth'.)
For more information, look up :h ft-python-indent.

Categories

Resources