Python reading and writing and decorator comprehensive practice

Table of contents

a. Write:

a.1: Open the file stu_info.txt in writing mode and write (name-sex-age) to the file: each line

b. Read:

b. Read: Open the file stu_info.txt by reading, read each line of the file, and format the output, output format: center alignment

decorator:

a. Decorator: It is required to define a decorator (general), which can print out the running time of the decorated function

function that satisfies the meaning of the question

prove


a. Write:

a.1: Open the file stu_info.txt in writing mode and write (name-sex-age) to the file: each line

use write

method one 

list_data = [   "zhangsan-male-20",
                "lisi-female-21",
                "wangwu-male-20"
            ]
file_data = open("stu_info", "w", encoding='utf-8')
str_data = file_data.write("\n".join(list_data))
file_data.close()

Method Two

list_data = [   "zhangsan-male-20",
                "lisi-female-21",
                "wangwu-male-20"
            ]
with open("stu_info", "w") as file_data:
    print(file_data.write("\n".join(list_data)))

 use writelines

method one

list_data = ["zhangsan-male-20\n","lisi-female-21\n","wangwu-male-20\n" ]
file_data2 = open("stu_info", "w")
str_data2 = file_data2.writelines(list_data)
file_data2.close()

 Method Two

list_data = ["zhangsan-male-20\n","lisi-female-21\n","wangwu-male-20\n" ]
with open("stu_info", "w") as file_data2:
    print(file_data2.writelines(list_data))

b. Read:

b. Read: Open the file stu_info.txt by reading, read each line of the file, and format the output, output format: center alignment

print(f"{'name':^20}{'gender':^10}{'age':^10}")
file_data = open("stu_info", "r")
str_data = file_data.readlines()
file_data.close()
# print(str_data, type(str_data))
for i in str_data:
    strip_data = i.strip()
    split_data = strip_data.split("-")
    # print(split_data, type(split_data))
    print(f"{split_data[0]:^20}{split_data[1]:^10}{split_data[2]:^10}")

'''
        name          gender     age    
      zhangsan         male       20    
        lisi          female      21    
       wangwu          male       20 
'''
'''

decorator:

a. Decorator: It is required to define a decorator (general), which can print out the running time of the decorated function

function that satisfies the meaning of the question

import time

def decorator(arg):
    def outer(func):
        def inner(*args, **kwargs):
            befor_time = time.time()
            func(*args, **kwargs)
            after_time = time.time()
            run_time = befor_time - after_time
            print(run_time)
        return inner
    return outer

@decorator("debug")

prove

Decorate print hello
import time

def decorator(arg):
    def outer(func):
        def inner(*args, **kwargs):
            befor_time = time.time()
            func(*args, **kwargs)
            after_time = time.time()
            run_time = befor_time - after_time
            print(run_time)
        return inner
    return outer

@decorator("debug")
def print_arg(*args, **kwargs):
    time.sleep(2)
    print("hello")
    # print(key=1)

print_arg(1, 3, key=2)
Decorate a function that takes keyword arguments
import time
def decorator(arg):
    def outer(func):
        def inner(*args, **kwargs):
            befor_time = time.time()
            func(*args, **kwargs)
            after_time = time.time()
            run_time = befor_time - after_time
            print(run_time)
        return inner
    return outer

@decorator("debug")
def print_key(*, key="vaule"):
    print(key)
    time.sleep(2)


print_key(key=1)
Decorate a function that can calculate and print the sum of three numbers
import time
def decorator(arg):
    def outer(func):
        def inner(*args, **kwargs):
            befor_time = time.time()
            func(*args, **kwargs)
            after_time = time.time()
            run_time = befor_time - after_time
            print(run_time)
        return inner
    return outer

@decorator("debug")
def print_sum(arg1, arg2, arg3, arg4):
    arg = arg1 + arg2 + arg3 + arg4
    print(arg)
    time.sleep(2)


print_sum(1, 2, 3, 4)

Guess you like

Origin blog.csdn.net/2302_77035737/article/details/130661075