Python-based character encoding and file processing

Source: Internet
Author: User
Tags throw exception

One, character encoding

(1) basic computer Knowledge

(2) How the Python interpreter executes the py file

<1>python Interpreter Launcher

The <2>python interpreter is equivalent to a text editor that opens the txt.py file and reads the contents of the txt.py file from the hard disk into memory

The <3>python interpreter interprets the code that was just loaded into memory txt.py (in that phase and execution, only the Python syntax is recognized, execution of the file memory code, execution to Name= "Egon", will open up memory space to hold the string "Egon")

(3) Similarities and differences between Python interpreter and text editor

the same point: The Python interpreter interprets the execution file contents, so the Python interpreter has the ability to read the Py file, which is the same as the text editor

Different points: The text editor reads the text content into memory for display, and the Python interpreter reads the contents into memory for execution.

Second, what is character encoding

The history of character coding

(1) The computer originated in the United Kingdom, the earliest birth was also based on the English-language considerations ASCII

ASCII: A bytes represents one character (English characters/all other characters on the keyboard), 1bytes=8bit,8bit can represent 0-2**8-1 variations, which can represent 256 characters

(2) In order to satisfy Chinese, the Chinese have developed gkb

Gbk:2bytes represents a character

(3) countries have national standards, there will inevitably be conflicts, the result is that in multi-language mixed text, the display will be garbled.

The resulting Unicode, unified 2Bytes for a character, 2**16-1=65535, can represent more than 60,000 characters, thus compatible with the universal language

Iv. Processing of documents

<1> File Open mode

The mode of opening the file is:

    • R, read-only mode "default mode, file must exist, not present, throw exception"
    • W, write-only mode "unreadable; not exist" created; empty content "
    • X, write-only mode "unreadable; not present, create, present error"
    • A, append mode "readable; not present, create; append content only"

"+" means you can read and write a file at the same time

    • r+, read and write "readable, writable"
    • w+, write "readable, writable"
    • x+, write "readable, writable"
    • A +, write "readable, writable"

"B" means to operate in bytes

    • RB or R+b
    • WB or W+b
    • XB or W+b
    • AB or A+b

Python-based character encoding and file processing

Related Article

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.