Python Gets the default encoding for the current environment

Source: Internet
Author: User
Tags locale
# CODING:GBK

Import sys
import locale

def p (f):
    print '%s.%s ():%s '% (f.__module__, f.__name__, F ()) 
 # returns the default character encoding
P (sys.getdefaultencoding) # used by the current system

returns the encoding p used to convert the Unicode file name to the system file name
( sys.getfilesystemencoding)

# Gets the default locale and returns the Ganso (language, encoding)
P (locale.getdefaultlocale)

# Returns the user-defined text data encoding
# documentation refers to this function only returns a Guess
p (locale.getpreferredencoding)

# \xba\xba is a ' Han ' GBK code
# MBCS is an deprecated encoding, where only testing shows why
print R "' \xba\xba '. Decode (' MBCS ') should not be used:", repr (' \xba\xba '. Decode (' MBCS '))

# Results on the author's windows (Locale to Chinese (Simplified, Chinese))
#sys. getdefaultencoding (): GBK
#sys. getfilesystemencoding (): MBCS
#locale. Getdefaultlocale (): (' zh_cn ', ' cp936 ')
#locale. getpreferredencoding (): cp936
# ' \xba\xba '. Decode (' MBCS '): U ' \u6c49 '

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.