找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
# python 当中的内置函数
# Python中的format函数
# format()函数是一种格式化字符串的函数,该函数增强了字符串格式化的功能
#基本语法:是通过{}来代替以前的%

# 1、不设置指定位置,按默认顺序
# a ="{} {}".format('hello','duoceshi')
# print(a)
#
# a = '{}{}'.format('hello','word')   #多输入{}也会报错
# print(a)

# 2、设置指定索引位置输出(索引位),只要有一个地方用了索引,所有的都要加索引
# a ='{0} {1} {1} {0}'.format('hello','word')
# print(a)

# 3、通过设置参数来格式化输出
# a = '{name} {age}'.format(name='xiaowang',age=18)
# print(a)

# 4、对列表进行格式化输出
# list1 = ['duoceshi','18']
# a = '{0}{0}'.format(list1)
# print(a)

# list1 = ['xiaowang','18']
# a = '姓名:{0[0]} 年龄:{0[1]}'.format(list1)
# print(a)

# 5、定义两个列表进行格式化输出
# list1 = ['xiaowang','18']
# list2 = ['dcs8']
# a = '{0[0]} {1[0]}'.format(list1,list2)
# print(a)

# 6、对字典进行格式化
# dict1 = {'name':'xiaowang','class':'dcs8'}
# a = '{name} {class}'.format(**dict1)
# print(a)

# dict1 = {'name':'duoceshi','class':'dcs8'}
# dict2 = {'age':'19'}
# a = "{0[name]} {0[class]} {1[age]}".format(dict1,dict2)
# print(a)

#zip函数,在接口测试里面用的比较多
# list1 = ['name','duoceshi','class','dcs']
# list2 = ['age','19']
# a = zip(list1,list2)
# print(a)
# # print(list(a))  #打印出来列表里面是一个个元组
# print(dict(a))  #返回的字典只要三个键值对,个数和迭代的列表元素最少的一致

#open()函数
#open()函数用于打开一个文件,创建一个file对象
#语法:open(file,mode),模式有r(只读),w(写入覆盖),a(写入追加)
#读的模式 r  read
# o =open(r'D:\Project\dcs8\lesson\aa.txt','r',encoding='utf=8')  #这个 o 就是文件对
#\a 表示专业符,路劲前面加r,边上不转义
# all =o.read()  #读取文件中所有内容,返回字符串类型
# all = o.readlines() #读取文件中所有的内容,返回列表,\n是换行
# all =o.readline()  #读取文件中第一行内容,返回字符串类型
# print(all,type(all))

#路径包含中文
# o = open(r'D:\Project\dcs8\lesson\aa.txt','r',encoding='utf-8')  #乱码加入条件
# all = o.readlines()
# print(all)

#写的模式 w write 有覆盖的作用
# o =open(r'D:\Project\dcs8\lesson\aa.txt','w',encoding='utf=8')
# # o.write('123')   #在文件中写入字符串,原来的数据会被覆盖,写入的数据不能为整型
# # o.writelines(['1','helo','3','56'])  #列表里面单个元素只能是字符串
# o.close() #关闭文件

#追加模式 a add
# o =open(r'D:\Project\dcs8\lesson\aa.txt','a',encoding='utf=8')
# o.write('123')
# o.writelines('\n123456')  #追加之前先换行
# o.writelines('中国\n')    #追加之后换行
# o.close()

#open()函数扩展用法,with open,每次执行完自动关闭文件对象
# with open(r'D:\Project\dcs8\lesson\aa.txt','a',encoding='utf=8') as f:
#     f.write('123\n')
#     f.writelines('321\n')
# with open(r'D:\Project\dcs8\lesson\aa.txt','r',encoding='utf=8') as f:
#     a = f.read()
#     b = f.readlines()
#     print(a)
#     print(b)

# python 中的time模块(导入模块的第一种方法,直接导入模块)
# import time
# # time 模块中常见的方法
# time() #1970年到现在经过的秒数
# ctime() #固定格式的当前时间
# sleep(3) #休眠  单位是秒
# asctime() #转换为asc码显示当前时间
# strftime() #时间格式化
# print(time.time())
# print(time.ctime())
# time.sleep(3)
# print(time.asctime())
# print(time.strftime('%Y-%m-%d-%H-%M-%S'))

#导入的模块的第二种方法:导入模块里面的函数
# from time import  sleep
# from time import  asctime   #通过模块导入了具体的函数
# from time import time as t  #帮函数取别名
# from time import ctime,asctime  #可以同时导入多个函数
# print(asctime())
# print(t())
# sleep(3)
# print(asctime())

#第三种导入模块方法:
# from time import *   #*号代表所有方法
# print(ctime())
# sleep(4)
# print(asctime())

# 导入模块的第四种方法:挎包导入
# from home.home1 import *   #这个是在当前模块调用home1模块
# print(num)
# print(list1)

#【python中random模块和os模块】
import random
# python中的random模块
# random模块中常见的方法:
# 1、random.random() #该方法是生成0-1之间的浮点数,但是能取到0,但是取不到1
# print(random.random())

# 2、random.randint(x,y) #该方法生成指定范围内整数,包括开始和结束值
# print(random.randint(1,6))

# 3、random.randrange(x,y,step) #生成指定范围内的奇数,不包括结束值
# 4、random.randrange(x,y,step) #生成指定范围内的偶数,不包括结束值
# print(random.randrange(1,10,2))   #取奇数,取不到10
# print(random.randrange(2,10,2))   #取偶数,取不到110

# 5、random.sample(seq,n) #从序列seq中选择n个随机且独立的元素
# list1 = [1,23,'tess','hello','dsca']
# print(random.sample(list1,3))

# 6、random.choice(test) #生成随机字符
# str1 = 'hellodcs'
# print(random.choice(str1))

# 7、random.shuffle(list) #洗牌(随机数列)
list1 = [1,23,'leis','defg','eefg']
random.shuffle(list1)
print(list1)

# 8、hashlib.md5() #MD5加密
# import hashlib
# md5 = hashlib.md5()
# md5.update('654321'.encode('utf-8'))
# print(md5.hexdigest())

# python 中的os模块
import os
#os 模块中常见的方法
#1、os.getcwd()获得当前执行命令所在目录的绝对路径
# print(os.getcwd())

# 2、os.path.isfile() 判断是否文件
# print(os.path.isfile('D:\Project\dcs8\lesson\lesson5.py'))

# 3、os.path.isdir()  判断是否是目录
# print(os.path.isdir('D:\Project\dcs8\lesson'))

# 4、os.path.exists()  判断文件或目录是否存在
# print(os.path.exists('D:\Project\dcs8\lesson\lesson5.py'))

# 5、os.listdir(dirname)   列出指定目录下的目录名或文件名,返回一个列表
# print(os.listdir('D:\Project\dcs8\lesson'))

# 6、os.path.split(name)   分割一个完整路径的最后一个文件或者目录
# print(os.path.split(r'D:\Project\dcs8\lesson'))

# 7、os.path.join(path,name) 链接目录与文件名或者目录
# print(os.path.join(r'D:\Project\dcs8\lesson','lesson5.py'))

# 8、os.mkdir(dir())  创建一个目录
# os.mkdir('D:\Project\dcs8\lesson\hello')

# 9、os.rename(old,new)  更改目录名称:
# os.rename('D:\Project\dcs8\lesson\hello','D:\Project\dcs8\lesson\hello12345')

# 【python中的re正则模块】
# 预定义字符集匹配:
# \d:数字0-9
# \D:非数字
# \s:空白字符
# \n:换行符
# \r:回车符
#
# re模块数量词匹配:
# 符号^:表示的匹配字符以什么开头
# 符号$:表示的匹配字符以什么结尾
# 符号*:匹配*前面的字符0次或n次
# eg:ab* 能匹配a 匹配ab 匹配abb
# 符号+:匹配+前面的字符1次或n次
# 符号?:匹配?前面的字符0次或1次
# 符号{m}:匹配前一个字符m次
# 符号{m,n}:匹配前一个字符m到n次(包括n次),m或n可以省略,mn都是正整数
import re
# 1、math 匹配的对象是字符串,匹配出来的结果也是字符串
#从第一个字符开始匹配,如果第一个字符不是要匹配的类型,则匹配失败并报错
#注意:如果规则带了'+',则匹配1次或者多次,无'+'只能匹配一次
# all = re.match('\d','helloword')  #打印报错,\d只能匹配数字,只是匹配1次
# all = re.match('\d','123helloword')  #结果为1,匹配1次
# all = re.match('\d+','123helloword')   #结果为123,匹配多次
# all = re.match('\d*','helloword')   #不会报错,匹配0次或者多次
# all = re.match('\d*','123helloword')  #结果为123,匹配多次
# all = re.match('\d?','helloword')   #不会报错,匹配0次
# all = re.match('\d?','123helloword')   #结果为1,匹配1次
# all = re.match('\d{3}','123helloword')  #匹配3次,结果为123
# all = re.match('\d{3}','12helloword')    #报错,必须匹配3次
# all = re.match('\d{1,3}','1hell34oword')  #结果为1,匹配1到3次,这里只匹配1次
# all = re.match('\d{1,3}','123hell34oword')  #结果为123,匹配1到3次
# all = re.match('\d{,3}','123hell34oword')   #结果为123,匹配3次
# all = re.match('\d{,3}','hell34oword')    #匹配0次,结果为空
# all = re.match('\d{,}','123453hell34oword')   #匹配6次,结果为123453
# all = re.match('\d{1,}','123hell34oword')      #匹配3次,结果为123
# all = re.match('\d{3,6}','12heoword')    #匹配报错,必须要匹配3到6次
# all = re.match('^1\d{1,5}','12345678')     #匹配以1开头,后面在匹配1到5次,结果为123456
# all = re.match('^132\d{2,5}','12345678')    # 报错,匹配以132开头,后面再匹配2到5次
# all = re.match('^132\d{2,5}','132345678')   #匹配以132开头,后面再匹配2到5次,结果为13234567
# all = re.match('^1\D{2,5}','1defef@!')     #\D匹配非数字,结果为 1defef
# all = re.match('^\D{2,5}','defef@!')  #匹配2到5次,结果为defef
# all = re.match('\D{2,5}','defef@!')     #匹配非数字2到5次,结果为defef
# all = re.match('\D{2,5}$','defef!')    #报错,$符合
# all = re.match('^1[358][5-9]\d{8}$','13866663333')
# #分四部分,匹配的字符串必须以1开头,第二位必须是358里的任意一个;
# #第三位必须在5-9之间任意一个,第四部分必须有8位
# all = re.match('^1[358][A-Z]\d{8}$','13A66663333')
# all = re.match('^1[358][a-z]\d{8}$','13a66663333')
# all =re.match('\D','2hello123de')  #报错,必须以非数字开头
# all =re.match('\D','hello123de')    #匹配1次,结果为h
# print(all.group())

# 2、search
#从第一个字符开始查找,一找到就返回第一个字符串,找到就不往下找,找不到则报错
# all = re.search('\d','hel12lo')  #匹配1次,结果为1,。匹配到数字就返回匹配不到就报错
# all = re.search('\d','hello')  #报错,匹配不到数字
# all = re.search('\d+','123hello321duoceshi')   #匹配多次,结果为123
# all = re.search('\D','123hello321duoceshi')   #匹配1次,结果为h
# all = re.search('\D*','hello321duoceshi')   #匹配非数字,遇到数字就结束匹配,结果为hello
# all = re.search('\D{2,5}','he321duoceshi')    #匹配连续非数字2到5次,结果为he
# all = re.search('\D{2,5}','h321duoceshi')   #匹配结果为duoce
# print(all.group())

# 3、findall
#从第一个字符开始查找,找到全部相关匹配为止,找不到返回一个列表[]
# all = re.findall('dcs(.+)dcs','dcshellodcs8')   #匹配括号里面的内容,两个dcs中间的内容
# all = re.findall('dcs(.+)dcs','dcshellodcsdcsworddcs8')   #匹配开头和结尾dcs中间的内容
# all = re.findall('\d','dcshellodcs128')  #匹配单个数字字符,结果:['1', '2', '8']
# all = re.findall('\d+','dcs34hellodcs128')   #多个连续字符匹配,结果为['34', '128']
# all = re.findall('\D','dcs3128')     #单个非数字字符,结果为['d', 'c', 's']
# all = re.findall('\D+','dcs3hehs128')   #多个连续字符匹配,结果为['dcs', 'hehs']
# all = re.findall('dcs','dcs3hehs128')   #匹配所有的dcs字符串
# all = re.findall('China','dcs3hehs128')   #匹配不到,返回一个空列表
print(all)
print(type(all))

# 4、compile
#编译模式生产对象,找到全部相关匹配为止,找不到返回一个列表[]
# all = re.compile('hello')  #先编译
# a = all.findall('dcshello123test')  #结果为['hello']
# # a = all.findall('dcs123test')      #[]
# print(a)

# 5、string 模块
# import  string
# num1 = string.digits   #获取0-9的数字
# num2 = string.ascii_letters   #获取大写写所有字母
# print(num1,type(num1))
# print(num2,type(num2))

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册