8.7 (distinguish three kinds of character encoding, python2 and 3 character encoded file is opened, with the upper and lower file management operations)

review

Data Classification

Variable immutable

Variable: lists, dictionaries, collections

Immutable: integer, floating point, string, a tuple

Order-disorder

Ordered: strings, lists, tuples

Disorder: dictionaries, collections

A plurality of values

Value: integer, floating point, string

A plurality of values: lists, tuples, dictionaries, set

Copy depth

Only for variable data types

Copy: Copy the object change, copy target is also changed

lt = [1,2,3,[4,5,6]]
lt2 = lt

# 当lt2为lt的拷贝对象时,lt内部任意数据类型的对象变化,lt2都变化

Shallow copy: copy target varies with the type of the variable data copied object changes

lt = [1,2,3,[4,5,6]]
import copy
lt2 = copy.copy(lt)  # lt2 = lt.copy()

# 当lt2为lt的浅拷贝对象时,lt内部可变数据类型变化,lt2也随之变化;lt内部不可变数据类型变化,lt2不变

Deep copy: both independent and copied objects how changes will not affect copies of objects

lt = [1,2,3,[4,5,6]]
import copy
lt2 = copy.deepcopy(lt)  

# 当lt2为lt的深拷贝对象时,lt内部任意数据类型的对象变化,lt2都不变化

Exception Handling

通用类型
try:
    1/0
except Exception as e:
    print(e)
finally:
    print(1)         不管对错都会输出
    
    
# assert
assert 1 == 1  # 满足条件跳过这一行 ; 不满足报错


# raise
raise 错误类型()  # 抛出异常

The basic file operations

打开:f = open(r'路径','r')
读写:f.read()   f.write()
关闭:f.close()#彻底关闭

Character Encoding

  1. cpu: controlling the operation of the program (text editor removed from the memory data is read into memory)
  2. Memory: (cpu data after the operation, the memory contains a text editor) to run the program
  3. Hard: storing data (text editor)

text editor:

Computer known only 0 and 1

Read and write data, save data

python interpreter:

Function has a text editor

010101011001010 -> a = 1 # 5 very ordinary character, is not running when the python interpreter provided # character encoding

Define the variable will open up the memory space to store variables, memory that is known only 0 and 1, a = 1 -> 01010110101 take some 0 and 1 # python2 he provided a string, when the python interpreter to run and the difference between 3 character encoding

Character Encoding:

A process between binary and characters you can recognize the conversion of

unicode: understanding language nations, nations coding

The English unicode 8 into a 16-bit English, takes up more memory space

Garbled:

Encoding format is not uniform

Text editor supports is utf8, you entered is gbk -> store files

File storage with the utf8, but the next time you open with the gbk -> read the file

Garbled two cases:

Chinese text editor only know Chinese, then you enter the Japanese -> garbled # encode coding

Text Editor stores Chinese (file), but you open the file with a Japanese editor -> # decode decode garbled

Solve the garbage:

What storage format, just read what format would not garbled (keep in mind these words)

windows notepad computer defaults to gbk encoding, in addition to other software default encoding is utf8

3 difference python2 and character encoding

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

python2 use the ascii, python3 default character is read utf8

coding: gbk` will tell python3 interpreter using the character gbk code reading

python3:

Actually see Unicode code, to help you put this terminal unicode zeros and do a conversion, it became a terminal that can be identified from the unicode format transcoding, and then turned into Chinese

Default encoding is assumed that the terminal gbk, encoded variables recognized unicode

Default encoding is assumed that the terminal utf8, unicode encoding variable awareness

python2:

encoding the specified coding unicode + (str type)

Terminal is a text editor that will be the default encoding.

Default encoding is assumed that the terminal gbk, gbk encoding the variable know

The default code is assumed that the terminal utf8, encoded variables do not know gbk

Open files three modes

# f = open('test.py','r',encoding='utf8')  # 只读  # 文件内容拿出来,读一行少一行
# # print(f.read())  # 读取文件所有内容,  # *****
# # print(1,f.readline())  # '''
# # print(2,f.readline())  #
#
# # 以后使用这个方式循环文件
#
# for i in f:
#     print(i)
#
# # print(f.readable())  # 是否可读  # True
# f.close()


# f = open('test.py','w',encoding='utf8')  # 只写  # 清空后再写
# lt = ['sdklfj','sdkfjksldf']
# res = '\n'.join(lt)
# f.write(res)  *****
# # print(f.readable())
# # print(f.writable())
# # f.writelines(['sdklfj','sdkfjksldf'])
# f.close()

f = open('test.py','a',encoding='utf8')  # 追加  # 文件末追加
f.write('nick handsome')  # *****
f.close()

python file context management

# 文本模式t
# f = open('test.py','rt')
# data = f.read()
# print(data)


# 进制模式b
# f = open('python2和3的字符编码的区别.png', 'rb')
# f = open('python2和3的字符编码的区别.png', 'wb')
# f = open('python2和3的字符编码的区别.png', 'ab')
# data = f.read()
# print(data)


import requests

res = requests.get('https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=4124192230,1955720178&fm=58.png')
img = res.content

print(img)

fw = open('test.png','wb')  # encoding是为了让二进制代码变成文本所需要的,所以b模式不需要encoding
fw.write(img)

with open('test1.png','wb') as fw:  # 自动关闭文件
    fw.write(img)


# with open('test.py', 'rt', encoding='utf8') as fr2, \
#         open('test.txt', 'rt', encoding='utf8') as fr1, \
#         open('test.png', 'rb') as fr3:
#     data1 = fr1.read()
#     print('data1', data1)
#     data2 = fr2.read()
#     print('data2', data2)
#     data3 = fr3.read()
#     print('data3', data3)
#
#
# print('skdfjlllllllsfsdfsdfskdfjlllllllsfsdfsdfskdfjlllllllsfsdfsdfskdfjlllllllsfsdfsdfskdfjlllllllsfsdfsdfskdfjll\
# 英')

Guess you like

Origin www.cnblogs.com/jiann/p/11317850.html