Python garbled, coded, Repr,encode,decode inquiry

Source: Internet
Author: User

#encoding: Utf-8
#用命令行执行

s = ' Baidu '
Print S # output environment is GBK, encoded as utf-8, output garbled
Print S.decode (' utf-8 ') # = Discovery Output Environment is GBK, automatic conversion
Print S.decode (' utf-8 '). Encode (' utf-8 ') # output environment is GBK, encoded as utf-8, output garbled
Print S.decode (' utf-8 '). Encode (' GBK ') # output environment is GBK, encoded as GBK, normal output


# s = 0xf21938274abds ... Binary memory
# Converting these memory data into a string that can be displayed is the print result of repr (s), and the non-displayed characters become like \x,
# repr (s) is not the memory data, nor is the memory data interpreted as a char to be displayed as chars.
# The meaning of Repr (s) is the result of print repr (s), which is the memory data is interpreted by char to be a realistic character
# if s = 0x24 = ' \ n ', then print repr (s) = ' \ n ', repr (s) is ' \\n ' (because you want to display \)
#
# s memory data according to a certain code interpretation, can get the correct meaning
# if S is in accordance with UTF-8 code ' haha ', with utf-8 interpretation can get ' haha ' meaning
# in Python the S is interpreted according to XYZ code, which is S.decode (XYZ)
# S.decode (XYZ) Gets a variable in python that doesn't care about its memory representation, perhaps "Data + encoding"
# no matter how it is expressed in memory, the abstract meaning of this variable can be extracted, we think it is abstract ' haha '
# An explanation of the string, can be converted to its encoding, is the meaning of the same, encoding change, get different binary data
# CMD output environment for GBK,UTF-8 encoded binary data directly in CMD print will get garbled
# UTF-8 encoded data is first decoded in utf-8 format meaning, and then encoded as GBK binary data, the cmd output is normal
# UTF-8 encoded data first decoded in utf-8 format meaning, direct output, Python will automatically detect the output environment
# automatically decode strings that know meaning according to the output environment


# Python in #encoding:xxx is a description of what format the Python code is decoded in
# General different coding English is the same can be recognized
# While the file is edited with the editor, we see the meaning when editing, and the editor uses an encoding to save the code meaning
# code meaning is saved by the editor as a binary data file in memory
# When the code file is executed by the Python interpreter, it looks for the #encoding:xxx, determines the code format encoding
# If the code is declared in a different way than the file is encoded, other than the English characters, the other is likely to be wrong
# Think of the Python interpreter with the B-code to explain the editor's meaning of a-coded preservation
# in general, the different encoding methods of Chinese are not compatible, so the interpretation of different encodings can cause garbled


# Popular Explanations
# What we use to communicate is the meaning of language, and different encodings are equivalent to writing in different languages to preserve meaning.
# Get a piece of text, according to the English grammar explanation is to use it in English format decoding. Python in decode
# Psychological think of a paragraph, written down in Chinese is to save it in Chinese code. Python in encode
# We interact with the input output is meaningful, if its output is not garbled, meaning the interaction succeeds, otherwise fail
# encoding is different from the software interpreter, the file editor, and the cmd command line they use in the language.
# That's what we want to make these software successful communication, in situations where they may use different languages.
In this way, we pass the meaning to the editor, the editor is written in a language, and the interpreter is read in a language.
# while the CMD output needs to be in B language, the interpreter will translate the language of a into meaning and translate it into a B language to cmd
# cmd get B language text, can translate its meaning, displayed on the screen, that is, no garbled output results



# So, if the above code is not executed in Cmd in the other Python IDE, you may get different results
Because the cmd will be GBK "language", while the other Python IDE may be directly utf-8, the garbled appearance of the situation is different

Python garbled, coded, Repr,encode,decode inquiry

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.