2019.8.7 learning content and notes

to sum up

Character Encoding

Review Computer Basics:

  1. cpu: running the control program (text editors to retrieve data read into memory from the hard disk)
  2. Memory: Run program (after CPU operation, a text editor, the data contained in memory)
  3. Hard: storing data (text editor)

text editor

Computer known only 0 and 1

The role of the text editor: read and write data, save data

Python interpreter

python interpreter with a text editing functions

First computer known only 0 and 1, when written in a binary: as

01010100111010 -> a = 1, when the python interpreter is not running ordinary characters 5

Define the variable will open up the memory space to store variable, because the memory is known only 0 and 1, so that after a string of characters to be converted in order to be recognized person

Character Encoding

A process of mutual conversion between binary and character you can know: the character encoding

Get up early with ascii code, because the programming language used in different countries, in order to make people able to use language in different countries can be edited, so the need for an IWC language (Unicode)

Under what circumstances will be garbled

Format is not uniform when garbled

Such as: text editor supports is utf-8, you entered is gbk -> file storage (garbled)

Storing the text utf-8, but with the next time you open gbk -> read the file (distortion)

Conclusion: When you open a text editor used, but the text does not enter or open the match, then there will be garbled

Solve the garbage

(Keep in mind): store what format, what format to read on, you will not be garbled

Supplementary: windows computer notepad default gbk coding, in addition to other software default encoding is utf-8

Process run code interpreter python

  1. Start python interpreter (the equivalent of a text editor)
  2. Open files, and display the detected character syntax (involving character encoding, a = 1 is just an ordinary character)
  3. Interpreted characters (character code relates, again the memory space, the production of the variable a = 1)

python2 and python3 difference:

python2 with an ascii coding, python3 default character read utf-8

coding: gbk gbk will tell python3 interpreter using the code reading characters

Learn python3

python3 with unicode character encoding

The default encoding is assumed that the terminal is gbk, understanding unicode encoded variable will not be garbled

The default code is assumed that the terminal utf-8, unicode encoding variable understanding, not garbled

Learn python2

python2 specified by coding the coding unicode + (str type)

Terminal is a text editor that will be the default encoding

The default encoding is assumed that the terminal is gbk, gbk coding understanding of variables, not garbled

The default code is assumed that the terminal utf-8, do not know gbk coding variables, there will be distortion

Three file open mode

  1. r: read, can only be read but not write, will complain when the file does not exist
 #文件内容拿出来,读一行少一行
print(f.read()) 读取文件内所有内容
循环:
for i in f:
    print(i)
f=open('price.txt','r',encoding='utf-8')
print(f.readable()) #判断是否可读  #true
f.close()
  1. w: write, can not only write read, write and then emptied
f=open('price.txt','w',encoding='utf-8')
f.write('nick handsome')
f.close
  1. a: append append, append at the end of file
f=open('price.txt','a',encoding='utf-8')
f.write('chen')
f.close

with file management context

Text mode t

f = open('test.py','rt')
data= f.read()
print(data)

Hexadecimal mode b (generally used for processing of the picture)

f = open('python2和3的字符编码的区别.png', 'rb')
data=f.read()
print(data)

Supplementary: with open (file contents such as: 'test.txt', 'rt', encoding = 'utf8') as fr1

with open and open differences:

  1. with open can open multiple files at once, you can continue to add to the open file format in the back as fr2 ..., separated by commas to indicate line breaks, this can be done with only one open can open multiple files
  2. with open file is automatically shut down, do not want to use f.close also open as to close the file

Guess you like

Origin www.cnblogs.com/chmily/p/11317981.html