8.8 (three forms of advanced application files, modify files in two ways, defined functions, defined function, the function's return value arguments, the function of the function)

review

Character Encoding:

The process of converting binary and character -> Character Encoding

ascii, gbk, shit, fuck each country has its own coding

US computer memory encoding is ascii; encoding Chinese computer memory is gbk, US computer does not recognize the Chinese computer program written in Chinese computer does not recognize the computer program written in the United States

Now the hard drive lying ascii / gbk / shit / fuck encoded files, they encoding format has not changed, so the emergence of unicode encoded in memory, unicode encoding memory can be identified ascii / gbk / shit / fuck encoded files

Run a unicode encoding the ascii / gbk / shit / fuck encoded files in the hard disk to finally charged with early unicode deposit into hard, but when he identification ascii, will be converted into 8-bit digital 16 digital to disk, a waste of space, so there utf8 (unicode and correspondence, and compression coding unicode characters)

utf8 coding to identify other countries, recognizes only unicode, utf8 is not currently in memory, but now the code is written in utf8, legacy ascii / gbk / shit / fuck encoded files disappear sooner or later / out of, or be converted to utf8 format, so sooner or later the memory is utf8.

python2 and 3 character encoding difference:

  1. Open the python interpreter
  2. python interpreter equivalent to the text editor, into common read binary characters a = 1
  3. After the conversion of ordinary characters to explain (define variables will open up a new memory space to store variable)
    python2
    variable with the file after encoding specified in the definition of storage
    if the specified file encoded as 'gbk', it will be stored in the form of variable gbk , originally printed are 0 and 1, but the terminal will automatically pair your 0 and 1 installation terminal default encoding converted into characters, if the default encoding terminal is utf8, garbled; if the terminal default encoding is gbk, do not mess it
    if you define plus u, coding ago variables: xxx will not have any impact on him, as it will use unicode code storage variable, the terminal is any type of coding can be identified
    python3
    with the unicode encoding variable storage definition
    later wrote what file format storage, in what format is read

The three file Open

r: read-only

f.read()

w. After emptying write (create a file does not exist)

f.write()

a. append (file does not exist is created automatically)

f.write()

Text mode: t binary mode: b

t / b can not be used alone, and can only be used with r / w / a

with file management down

with open () as f: # automatically shut down the

pyinstallerde use

pip instlal pyinstaller

Switching path to the folder (folder contains two files img.ico and test.py)
pyinstaller -i img.ico -F test.py

Advanced application file

r+t:可读可写
with open('test.py','r+',encoding='utf8') as fr:
#     data = fr.read()
#     print(fr.writable())
#     fr.write('x = 10')
#     print(data)
#   r+既可读又可写
w+t:可写可读
w会清空文件,w+尽量不要使用  可写可读
# with open('test.py','w+',encoding='utf8') as fw:
#    print(fw.readable())
#    fw.write('x = 10')
#    data = fw.read()
#    print(data)
a+t:可追加可读
with open('test.py','a+',encoding='utf8') as fa:
#     fa.seek(1,0)
#     data = fa.read()
#     print(data)
#
#     fa.write('x = 10')
#     fa.flush()刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区
#    指针的应用
指针使用
seek(按字节位移)
# with open('test.py','r+',encoding='utf8') as fr:
    # fr.seek(1)   #1表示位移一位,默认从文件头开始
    # fr.seek(1,0) #0表示从头开始
    # fr.seek(4,1) #1表示从当前位置开始
    # fr.seek(0,2) #2表示从文件末尾开始
    # print(fr.read())
#    tell(字节)当前指针位置
# with open('test.py','r+',encoding='utf8') as fr:
#     fr.seek(2,0)
#     print(fr.tell())
#    read(n)字符读多少个字符
# with open('test.py','r+',encoding='utf8') as fr:
#     print(fr.read(9))
#    truncate()截断,字节
# with open('test.py','r+',encoding='utf8') as fr:
    # fr.truncate(5)   清空后面的

Files modified in two ways

The contents of the file stored in the hard disk all loaded into memory, the memory can be modified, the modification is completed, and then covered by the memory to the hard disk

with open('test.py', 'r+', encoding='utf8') as fr:
#     data = fr.read()
#     fr.truncate(0)
#     data = '中中中下下下中中水电费中'
#     print(data)

# import os
# #
# # # 文件没有修改这一说,只有覆盖这一说
# # with open('test.py', 'r', encoding='utf8') as fr, \
# #         open('test_swap.py', 'w', encoding='utf8') as fw:
# #     data = fr.read()
# #     data = data.replace('sb', 'dsb')
# #
# #     fw.write(data)
# #
# # import time
# # time.sleep(5)
# # os.remove('test.py')  # 删除文件
# # os.rename('test_swap.py', 'test.py')  # 重命名文件

The contents of the file stored in the hard disk into memory line by line, the modification is completed on the new file is written, and finally cover the source file with a new file (read side becomes write)

import os

# 文件没有修改这一说,只有覆盖这一说
with open('test.py', 'r', encoding='utf8') as fr, \
        open('test_swap.py', 'w', encoding='utf8') as fw:
    for i in fr:
        i = i.replace('sb', 'dsb')
        fw.write(i)

os.remove('test.py')  # 删除文件
os.rename('test_swap.py', 'test.py')  # 重命名文件

Defined functions

# 定义函数的方式
'''
def 函数名():  # 定义阶段
    """函数注释写在这里""" 
    <代码块>
# 使用  # 调用阶段(开车阶段)
函数名()
'''
# 定义阶段不执行函数体代码,只检测语法错误
def func():
    """func函数的注释撒地方撒地方撒地方"""
    # todo:未来写一个开车函数
    pass
print(login.__doc__)  # 记住   可以输出注释
# 字符串:split   列表:append   字典:get  集合:add
# read(), wirte()

Function three definitions of way

Parameters: unknown amount, variable

No reference function: alone

def add():
    """无参函数"""
    x = input('num1:')
    y = input('num2:')
    print(int(x) + int(y))

There are function parameters:

def add(x, y):  # 我这个工具不能单独使用,必须得加个配件,才能使用
    """有参函数"""
    print(int(x) + int(y))
print(1)
x = input('num1:')
y = input('num2:')
add(x, y)

Empty function: only defines a function

def func():
    pass

Call functions

def add(x, y):
    return x+y


add()
res = add()
print(res)  

Function's return value

return  返回函数值,跳出函数
可以返回任意数据类型   无个数限制
0   返回None
1   返回该值本身
多个   返回值是元组

Function parameters

Parameter: the parameter in parentheses defined function definition phase, called the formal parameters, parameter referred to, is essentially the variable name

Arguments: passed in the function call stage parentheses parameters, known as actual parameters, referred to the argument, in essence, is the value of the variable

Location parameter:

在函数定义阶段,按照从左到右的顺序依次定义的形参,称之为位置形参。
def func(x, y):
    print(x)
    print(y)
特点:按照位置定义的形参,都必须被传值,多一个不行,少一个也不行

Location argument:

在函数调用阶段,按照从左到右的顺序依次定义的实参,称之为位置实参。
func(1, 2)
特点:按照位置为对应的形参依次传值

Keyword argument:

When you call the function, parameter by value in the manner specified key-value pairs,

Can break position limit, can participate assigned to the specified shape

Precautions:

  1. You can mix position arguments and keyword arguments, but the location argument must be the keyword arguments left.
  2. Position and can be mixed arguments keyword arguments, but can not repeat the assignment for a parameter.

The default parameter:

Definition of when he is assigned, you can not assign a value when calling

Precautions:

  1. Location parameter must be placed in the default parameter left.
  2. The default parameter values ​​assigned only once in the definition phase, which means that the default value of the parameter in the function definition phase has been fixed

Guess you like

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