R
R
Radiss2020-03-08 03:16:52
Python
Radiss, 2020-03-08 03:16:52

How to change encoding in windows console?

The file should be output in utf-8, and in the console - 866, as a result diamonds are displayed in the browser.

Nothing changed after the chcp 65001 command.

Since the console uses code page 866, if you change the value of the REG_SZ parameter "866" in the registry under the key [HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage] from "C_866.nls" (by default) to something else, then the encoding in cmd should also change.

But I don't have such files in CodePage. There are REG.SZ types by default and 4 files with numbers 932 936 949 950

The option to constantly change in the chcp console is not suitable, but it does not work either. Lucida console is connected to the console. Cygwin64 Terminal and Gitbash won't start python server

Any other options?

generate.py

spoiler

# coding: utf-8

from horoscope import generate_prophecies
from datetime import datetime as dt


def generate_page(head, body):
    page = f"<html>{head}{body}</html>"
    return page


def generate_head(title):
    head = f"""<head>
    <meta charset='utf-8'>
    <title>{title}</title>
    </head>
    """
    return head


def generate_body(header, paragraphs):
    body = f"<h1>{header}</h1>"
    for p in paragraphs:
        body = body + f"<p>{p}</p>"
    return f"<body>{body}</body>"


def save_page(title, header, paragraphs, output="index.html"):
    fp = open(output, "w")
    today = dt.now().date()
    page = generate_page(
        head=generate_head(title),
        body=generate_body(header=header, paragraphs=paragraphs)
    )
    print(page, file=fp)
    fp.close()

#####################


today = dt.now().date()

save_page(
    title="Гороскоп на сегодня",
    header="Что день " + str(today) + " готовит",
    paragraphs=generate_prophecies(),
)



horoscope.py

spoiler

# coding: utf-8

from horoscope import generate_prophecies
from datetime import datetime as dt


def generate_page(head, body):
    page = f"<html>{head}{body}</html>"
    return page


def generate_head(title):
    head = f"""<head>
    <meta charset='utf-8'>
    <title>{title}</title>
    </head>
    """
    return head


def generate_body(header, paragraphs):
    body = f"<h1>{header}</h1>"
    for p in paragraphs:
        body = body + f"<p>{p}</p>"
    return f"<body>{body}</body>"


def save_page(title, header, paragraphs, output="index.html"):
    fp = open(output, "w")
    today = dt.now().date()
    page = generate_page(
        head=generate_head(title),
        body=generate_body(header=header, paragraphs=paragraphs)
    )
    print(page, file=fp)
    fp.close()

#####################


today = dt.now().date()

save_page(
    title="Гороскоп на сегодня",
    header="Что день " + str(today) + " готовит",
    paragraphs=generate_prophecies(),
)



When you run the code (python generate_all.py from the command line or Ctrl + B in the sublime), an index.html file is generated in the same folder, and if you raise the server in the same directory (python -m http.server) from the win console, then diamonds in the browser.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AUser0, 2020-03-08
@radiss

If you have some program that runs in the console, writes something to a file in some kind of encoding, then you need to deal with this program. With a simple 'chcp 65001' you only change the encoding of the console itself. Well, there are programs that do understand the encoding setting, and display the text in the specified encoding. If the program you need spits on this setting from a high bell tower, then it is useless to change it. It's easier in the browser to manually switch to the correct encoding. You just have to do it manually each time you open the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question