python road seventh day


First, the character encoding:

For all of the text

Character input translated into computer can identify a binary number by a corresponding character code table.

In fact, for the computer, just some of the characters corresponding to binary numbers.

The character encoding of development:

Character code table beginning with the ASCII code. To represent a binary with eight English characters.

After settling time, after which each country has its own set of character encoding tables. For example our own use GBK coding table.

And later, after some vicissitudes of history, the world has developed a Unicode character encoding table, all countries can decode the character, the character will no longer have to worry about not identified.

However, Unicode has a fatal drawback is the low efficiency program, and a waste of storage space.

However, when the memory of the Unicode character encoding format hard disk read time, we will follow the UTF-8 encoding, that will save space.

Now the character encoding of computer memory tables are Unicode, while the hard disk is UTF-8.

Features Unicode character encoding tables summarize:

when 1. The user then inputs, no matter what the character is, it can be compatible.

2. When the relationship between the encoded data in different countries reached when a hard disk memory, has a corresponding upper Unicode.


Character Encoding Summary:

When data is stored to the hard disk from the memory:

Unicode encoding format to save memory the binary data 1. ------------ by ENCOD (encoding) ----- binary data encoding format UTF-8 to the hard disk -----

When the data is read from the hard disk into memory:

1. The binary data encoding format UTF-8 in the hard disk is read into memory by --------- decode (decoding) the Unicode encoding format binary data ----- ----

What encoded text files compiled, with regard to what encoding solution

Second, the file header

When writing python programs, files the first to write a coding: utf-8

When you do not mark the file header, the interpreter will default to its own format for storing data such as python2 ---- will be stored in ASCII code.

All support English character encoding.

In the python interpreter as long as when used in front of the Chinese need to add the letter u


Third, file handling and processing modes:

1.open Open

r ---- unescaping

x = open (r'D: \ feiq \ Recv Files \ day07 \ test ', encoding =' utf-8 ') ----- as the operating system sends a request, if it is assigned to a variable name, equivalent to a remote control to operate.

x.read () as the operating system sends a request to read the file contents.
x.close () allow the operating system to close open files

2.with operation file context.

with open(r'......................','utf-8') as d,\
open (r'......................','utf-8') as d1:

3.write write
readable readable determines whether
whether writetable Analyzing writable

Open the two files, when you're outside with the hierarchy, the file will be closed.


4 file open mode

1. mode is r, read-only mode, and a one-time read all the contents of the file. You can not modify the file. If you do not write r, r default mode.

When 2.mode is w, write-only mode, when the file is not automatically created the file. When the file exists, it will first clear the original contents of the file and then write.

When 3.mode is a, it is an additional write mode, when the files are not automatically create the file. When the file exists, the original file will continue to the end of writing.


5. Unit operation mode of file:

t refers to a text file. You need to specify the encoding parameters. mode parameters do not write it, the default is t.

b is binary. Must not specify the encoding parameters, will write the general parameters other file transfer

 

 

 

Life is short, python and song

 

Guess you like

Origin www.cnblogs.com/wujc3/p/11140666.html