Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the encoding of the terminal from within a python script?

Let's say you want to start a python script with some parameters like

python myscript some arguments

I understand, that the strings sys.argv[1] and sys.argv[2] will have the encoding specified in the terminal. Is there a way to get this information from within the python script?

My goal is something like this:

terminal_enocding = some_way.to.GET_TERMINAL_ENCODING
some = `sys.argv[1]`.decode(terminal_encoding)
arguments = `sys.argv[2]`.decode(terminal_encoding)
like image 404
Aufwind Avatar asked Jun 18 '11 14:06

Aufwind


People also ask

How do you define encoding in Python?

Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points. For efficient storage of these strings, the sequence of code points is converted into a set of bytes. The process is known as encoding.

What is Unicode in PYTHON?

Unicode is also called Universal Character set. ASCII uses 8 bits(1 byte) to represents a character and can have a maximum of 256 (2^8) distinct combinations.


1 Answers

sys.stdout.encoding will give you the encoding of standard output. sys.stdin.encoding will give you the encdoing for standard input.

like image 162
rafalotufo Avatar answered Oct 26 '22 20:10

rafalotufo