Home python How to set UTF-8 encoding in Python when generating a file?

How to set UTF-8 encoding in Python when generating a file?

Author

Date

Category

Here is the source code of the generator file on Python. I can not configure the correct encoding when writing to the file, writes to Krakozyab

from outfile import outfile_text
From DateTime Import DateTime AS DT
DEF Generate_page (Head, Body):
  Page = "& lt; HTML LANG = 'RU' & GT;" + Head + Body + "& lt; / html & gt;"
  Return Page
Def Generate_Head (Title):
  Head = "& lt; Meta Charset = 'UTF-8' & GT;" + "& lt; Title & gt;" + title + "& lt; / title & gt;"
  Return "& LT; Head & GT;" + Head + "& lt; / Head & gt;"
Def Generate_Body (Header, Paragraphs):
  Body = "& lt; h1 & gt;" + Header + "& lt; / h1 & gt;"
  For P in Paragraphs:
    BODY + = "& LT; P & GT;" + p + "& lt; / p & gt;"
  Return "& Lt; Body & GT;" + Body + "& lt; / body & gt;"
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 = "Title1",
  Header = "Today" + STR (Today),
  Paragraphs = generate_texts (),
)

tried the Solution written here The output of the UTF-8 CGI script on Python 3 does not help.
Also they write that you need to configure Apache. Can I configure it in OpenServer or for Python there is your Apache?
Python 3.7 Version

Final file

& lt; html lang = 'ru' & gt;
& lt; Head & gt;
  & lt; Meta Charset = 'UTF-8' & GT;
  & lt; Title & gt; × Òî î îñïïû & & LT; / Title & gt;
& lt; / Head & gt;
& lt; Body & gt;
  & lt; h1 & gt; × × äÅíÜ 2019-10-30ãòÒ & lt; / h1 & gt;
  & lt; p & gt; â ÷ å å î îæèäàààòèäããÃîãããããããããããããããããêêêêê Ïñáàà îæääà òåäà ìììììèèèìèèè .è... .È ÏåååìììäääààààààääãÃîããããããããããããããããã &ã &ããããããããããã & / / / P & GT;
  & lt; p & gt; á üüòòòòòòòãããããããããÃîããããããããããããããããããããããããã  ÷ åðèèèÀÉÒåå è èèèèèèèèèèèè .èè .è .è .èè  ÷ åèèèàà àààääÃîãÃîããÃîããããèèèèêà & · / P & GT;
  & lt; p & gt; ïñá îáäà îèäà ãñääããããããããããããããããããããããîããîã Äíììääããééñññññ ññòòòé çããÃîãããããããããããîãããîããîîãî Ïååäìììäàààààåååèèèìèèèèèèèè................. .È .ìììììììììììììììììììììì ñ
  & lt; p & gt; ï ïíäìììääàààààãããããããããããããããããããããããããã Equipment Equipment. Equity Equipment ÃîñãÃîéããããããããããããããããããã .ãã .ãããããããîîîîîîîîîîîîîîîîîîîîîîîî / / / /î /îÒ
  & lt; p & gt; ä¸ììèàà degeneration. ÏñáàÀ Áóäüòåå õõõõõõõõååååååååååååå .åå....... .Å Á üüüòòòãããããããèêêêêàêàêêääãããããããããããããããããããããããããããããããããããããããããããîãããã
& lt; / body & gt;
& lt; / html & gt;

Answer 1

The network has a sufficient number of online services that allow you to decipher the cracks into the text and simultaneously define encodings. This will help determine what is not like encoding. For example, the decoding of the text of one of the paragraphs allows you to determine that the original text in the CP1251 encoding encoding is displayed in the CP1252 encoding encoding. To avoid this, you need to specify the encoding when you open the file.

specifically in your case, you need to change the Save_page function as follows:

def save_page (title, header, paragraphs, output = 'index.html'):
  With Open (Output, Mode = 'W', Encoding = 'UTF-8') AS FP:
    Today = dt.Now (). date ()
    page = generate_page (
      Head = Generate_Head (Title),
      Body = Generate_Body (
        Header = Header,
        Paragraphs = Paragraphs.
      )
    )
    Print (Page, File = Fp)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions