Linux system encoding, Python encoding, file encoding

Source: Internet
Author: User
Tags locale

1     Preface

If you are clear about the Python2 and Python3 decoding, here I think you know.

Specific reference documents:

"Python2 encode and decode function Description . docx"

"character encoding--from ASCII start . docx"

All of the above documents are local documents.

2     PythonCoding

Sys.getdefaultencoding (): Gets the current encoding of the system, where the system refers to python 's own built-in system, not the operating system, which is 3 in the python Coding .

Sys.setdefaultencoding (): set system default encoding, do not see this method when executing dir(sys), donot pass in interpreter, can execute reload (SYS) First , thesystem default encoding is set to UTF8 when setdefaultencoding ('UTF8') is executed.

Sys.getfilesystemencoding (): Get File system using encoding method,return ' MBCS ' underWindows,mac back 'utf-8 '.

This section can be found in the " Python determines the current operating system type and OS, sys, platform module Introduction "

In fact, I think this part of the back of the fault location has no effect.

3     system Code,Pythonencoding, file encoding3.1   system Code

The default write source Editor encoding method . It means that everything inside the source file is encoded into a binary stream based on this method. Saved to disk. Linux is viewed under the locale command.

This part of the code is called the editor's code, such as the VI command.

add: If you copy the text from Linux directly from other places on the Linux terminal, it will be encoded by the system encoding and viewed by the locale command:

[[Email protected]~]$ locale]

Lang=gbk

Lc_ctype= "C"

Lc_numeric= "C"

Lc_time= "C"

Lc_collate= "C"

lc_monetary= "C"

Lc_messages= "C"

Lc_paper= "C"

Lc_name= "C"

Lc_address= "C"

Lc_telephone= "C"

Lc_measurement= "C"

lc_identification= "C"

Lc_all=c

[Email protected]]$ more env.sh

#!/bin/bash

Export Lc_all= "en_US. UTF-8 "

Export lang= "en_US. UTF-8 "

[Email protected]]$ Source env.sh

[[Email protected]]$ locale]

Lang=en_us. UTF-8

Lc_ctype= "en_US. UTF-8 "

Lc_numeric= "en_US. UTF-8 "

Lc_time= "en_US. UTF-8 "

Lc_collate= "en_US. UTF-8 "

Lc_monetary= "en_US. UTF-8 "

Lc_messages= "en_US. UTF-8 "

Lc_paper= "en_US. UTF-8 "

Lc_name= "en_US. UTF-8 "

Lc_address= "en_US. UTF-8 "

Lc_telephone= "en_US. UTF-8 "

Lc_measurement= "en_US. UTF-8 "

Lc_identification= "en_US. UTF-8 "

Lc_all=en_us. UTF-8

3.2   PythonCoding

means python How to decode the internal settings . If not set,python defaults to the ASCII decoding method. If the Python source code file does not appear in Chinese, how to set this place should not be a problem. Setting method: At the beginning of the source file (must be the first line):#-*-coding:utf-8-*-, the source file is set to decode the way is UTF-8

3.3   file Encoding

the encoding of the text,vim under Linux using set fileencoding view.

Note When using this command to view the encoding, you need to ensure that locale under the

Lc_all= "en_US. UTF-8 "

Lang= "en_US. UTF-8 "

It's the same as when you coded the file, otherwise it won't show.

PS: At that time I VI sendmail.py file encoding is Utf-8, that is, the locale is displayed en_US. UTF-8;

[[email protected] python3]$ ls

Env.sh sendmail.py

[[Email protected]]$ locale]

Lang=gbk

....

Lc_all=c

This environment goes on. View File Encoding:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/95/AB/wKioL1kYVbiTKgKZAAB1PfiJUvg804.png-wh_500x0-wm_ 3-wmp_4-s_3322431081.png "style=" Float:none; "title=" 1.png "alt=" Wkiol1kyvbitkgkzaab1pfijuvg804.png-wh_50 "/>


Chinese display is garbled, use setfileencoding to see no output at all:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/95/AB/wKiom1kYVbjA3fupAACAl5VVuVg864.png-wh_500x0-wm_ 3-wmp_4-s_4177827363.png "title=" 2.png "style=" Float:none; alt= "Wkiom1kyvbja3fupaacal5vvuvg864.png-wh_50"/>

Environment Check back after the preliminary

[Email protected]]$ Source env.sh

[[Email protected]]$ locale]

Lang=en_us. UTF-8

...

Lc_all=en_us. UTF-8

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/95/AB/wKioL1kYVbnxeKpKAACFKDKAlaI397.png-wh_500x0-wm_ 3-wmp_4-s_1171430744.png "title=" 3.png "style=" Float:none; alt= "Wkiol1kyvbnxekpkaacfkdkalai397.png-wh_50"/>

4     Summary

You are a good programmer, do not recommend to move the server's code, unless the server only run your own program.


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1925618

Linux system encoding, Python encoding, file encoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.