+ 4

How to useUTF-8 in python?

As we did in html to specify the encoding to be utf-8, How should we do that in python. Is it not necessary?

24th Dec 2018, 9:07 PM
Rahul Gill
Rahul Gill - avatar
11 ответов
+ 9
Using sys.stdout.reconfigure(encoding="utf-8")
26th Dec 2018, 10:16 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
Because python strings are unicode, I believe that UTF-8 is only required in older systems where letters are still handled as ASCII using 7 bits. That may be the case with any language. I may be wrong. strings has encode method that you can use. "Hello world".encode("utf8");
24th Dec 2018, 9:54 PM
Toni Isotalo
Toni Isotalo - avatar
+ 4
Like Choubada said, UTF-8 is the default encoding in recent versions, so it's not needed. From the docs: "If no encoding declaration is found, the default encoding is UTF-8." https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations However, to print UTF-8 or UTF-16 characters, depending on the terminal defaults, it may be necessary to add something to your code: https://code.sololearn.com/c38hmKFESSf2/?ref=app
25th Dec 2018, 3:14 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
On Windows 10, Python 3.8? I've had to include Adam Lee's # -*- coding: utf-8 -*- ..as the top line of my codes in order to read/write text files containing characters with ordinal > 127. Later I switched to using the files in binary modes instead. [Note] The 'strict' parameter in any extended character set (utf8 through utf32) refers to these error handlers: https://docs.python.org/3/library/codecs.html#error-handlers [codecs vs wrapper] Discovery that extended chars worked here was via the "codecs" import but... ...while researching a potential bug I discovered that TextIOWrapper (as Flandre Scarlet is using) was way more capable (I prefer it) [Trivia] codec = COder/DECoder [Unicode HOWTO] https://docs.python.org/3/howto/unicode.html
25th Dec 2018, 8:57 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
For the most recent versions, python is automatically in utf-8 so you don't need to specify it
24th Dec 2018, 10:22 PM
Chalza
Chalza - avatar
+ 2
You are welcome, Vitalij Bredun ♡ JUMP_LINK__&&__Python__&&__JUMP_LINK Petersburg ! I'm not sure what you mean by the second question. If Adam wishes to discuss more on this specific topic, he can certainly ask here! But if it diverges significantly from the main question of the post, it may be more suitable to make a separate one. Activity feed posts and direct messages are also options to consider. If the question is about Kuba's code specifically, a comment in the code may be a good idea.
25th Dec 2018, 5:23 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Got its answer on stack overflow # -*- coding: utf-8 -*-
24th Dec 2018, 10:05 PM
Rahul Gill
Rahul Gill - avatar
+ 1
Kishalaya Saha pls say a ideas where you advice to post this ask of Adam Lee if this question was discussed but Adam wish to talk talk talk more? Talking is a useful way to understand something.
25th Dec 2018, 4:44 AM
Vitalij Bredun ♡ Python Petersburg
Vitalij Bredun ♡ Python Petersburg - avatar
+ 1
Kirk Schafer, for files, if you used the encoding keyword of the open() function (reference: https://docs.python.org/3/library/functions.html#open ), did you still have to use the declaration at the top?
26th Dec 2018, 11:01 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
Kishalaya Saha Thx for your link on the wonderful code.
25th Dec 2018, 4:41 AM
Vitalij Bredun ♡ Python Petersburg
Vitalij Bredun ♡ Python Petersburg - avatar