双子座自动化测试day4
作者:互联网
本次博客内容记录第三次自动化测试课程(2019.03.29)的主要知识点,主要包括python基础中的元组,集合,文件读写及函数的使用
一.元组
# 元组是不可变的list t = ('127.0.0.1',3306,'root','123456') t.index(3306) t.count(3306)
二.集合
# 集合天生可以去重 # 集合是无序的 s1 = {1,2,3,4} s1.add('abc') s1.remove('abc') # l = [1,2,3,3,4,4] # res = set(l) # print(res) #定义空集合 # s = set() # 交集 s1 = {1,2,3,4} s2 = {4,5,6,7} # print(s1.intersection(s2)) # # stu1 = {'zhaozhulin','ylm','dxl','xuzhan'} #自动化班 # stu2 = {'ylm','dxl','zlj','djsd'} # 性能班 # print(stu1 & stu2) # 并集 print(s1.union(s2)) print(s1 | s2) #差集(s1里面有,s2里面没有) print(s1.difference(s2)) print(s1-s2) #对称差集(把s1与s2里面都有的元素去掉) print(s1.symmetric_difference(s2)) print(s1^s2) #要求 必须包含大写字母,小写字母,数字,特殊字符 # 非空即真,非0即真 # import string # password = input('请输入密码:').strip() # # # string.ascii_uppercase # # string.ascii_lowercase # # string.digits # # string.punctuation # # p_set = set(password) # if p_set.intersection(set(string.ascii_uppercase)) and p_set.intersection(set(string.ascii_lowercase))\ # and p_set.intersection(set(string.digits)) and p_set.intersection(set(string.punctuation)): # print('密码合法') # else: # print('密码必须包含大写字母,小写字母,数字,特殊字符')
三.文件读写
1.常规应用方法
# open('a.txt') # file('a.txt')# python2 # f = open('a.txt','r',encoding='utf-8') # r只能读不能写,文件不存在时会报错 # f = open('a.txt','w',encoding='utf-8') # w文件不存在时会自动创建,不能读,会清空原来的文件内容 # f = open('a.txt','a',encoding='utf-8') # a追加模式,不会清空原来内容,但是不能读;文件不存在时会自动创建 # r+ 读写模式 # f = open('user.txt','r+',encoding='utf-8')# 不存在不会创建,可读写,写的时候有问题 # f1 = f.read() # print(f1) # f.write('123') # w+ 写读模式,会清空原来内容,可以读 # a+ 追加读模式,能写能读,读不到东西 # f = open('c.txt','a+',encoding='utf-8') # 文件指针 # f = open('user.txt',encoding='utf-8') # # print(f.readline())# 读取一行 # # print(f.readline()) # print(f.readlines())# 读取文件所有内容,放在一个list # print(f.read())# 读取文件所有内容,字符串形式;此处由于readlines读取完后,指针在文件最后,导致再次read后为空 # 移动文件指针 # f = open('user.txt','a+',encoding='utf-8') # f.seek(0) #把文件指针移到最前 # print(f.read()) # writelines:把list里面的每一个元素写入文件 # l = ['1\n','234\n','asd\n'] # f.writelines(l) #从文件指针的位置开始删除内容 # f = open('user.txt','a+',encoding='utf-8') # f.seek(0) #把文件指针移到最前 # print(f.tell())# 当前文件指针的位置 # f.truncate() # with打开文件不用手动关闭 # with open('a.txt') as f,open('b.txt') as b: # f.read() # 打开大文件时,为了避免内存条过满导致死机,需逐行读取 # with open('score.txt',encoding='utf-8') as f: # for line in f: # print('每次循环的内容是:',line)
2.修改文件方法
# 把names文件中的s替换成S # f = open('names.txt','a+',encoding='utf-8') # f.seek(0) # res = f.read().replace('s','S') # f.seek(0) # f.truncate() # f.write(res) # 1.简单粗暴方式:先读取所有,修改,清空原文件,写入修改后的内容 # f = open('account.txt','a+') # f.seek(0) # l = [] # for line in f: # line = line.strip() # line_list = line.split(',') # # ['abc',80,90,100] # # line_list[1:] # sum = int(line_list[1])+int(line_list[2])+int(line_list[3]) # avg = sum//3 # new_line = '%s,%s\n'%(line,avg) # # print('new_line:'new_line) # l.append(new_line) # f.seek(0) # f.truncate() # f.writelines(l) # 2.高效的方式:打开两个文件,逐行读a文件,处理后写入b文件 import os with open('account.txt') as f,open('account2','w') as fw: for line in f: line = line.strip() line_list = line.split(',') # ['abc',80,90,100] # line_list[1:] sum = int(line_list[1])+int(line_list[2])+int(line_list[3]) avg = sum//3 new_line = '%s,%s\n'%(line,avg) fw.write(new_line) fw.flush() # 立即写入文件(不写此步骤有可能由于内存缓冲区未满,导致未及时写入磁盘) os.remove('account.txt') os.rename('account2','account.txt')
四.函数基础1
# def hanshu(): # print('这是一个函数') # # hanshu() # 函数:实现某个功能的代码 # def calc(a,b): # 形参,形式参数 # print(a,b) # return a+b # 返回值 # # result= calc(1,2) # 实参,实际参数 # print(result) # 判断输入的是否是合法的小数 # 1.判断是否只有一个小数点 # 2.小数点左边是一个整数,小数点右边也是一个整数 # 3.小数带你右边是整数,小数点左边以负号开头,且只有一个负号,负号右边是整数 import string # num = input('请输入价格:').strip() # def check_float(num): # num = str(num) # if num.count('.')==1: # left,right = num.split('.') # if left.isdigit() and right.isdigit(): # return True # elif left.startswith('-') and left[1:].isdigit() and right.isdigit(): # return True # return False # 函数里遇到return 立即结束 # print(check_float(-1.5)) # print(check_float(1.5)) # print(check_float('s.1')) # print(check_float('1.s')) # print(check_float('1.')) # print(check_float('.'))
标签:day4,list,print,自动化,双子座,line,txt,open,s1 来源: https://www.cnblogs.com/zzl991/p/10738648.html