找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

python中的内置函数
               python中的format函数
               format()函数时一种格式化字符串的函数,该函数增强了字符串格式化的功能
               基本语法:是通过{}来代替以前的%

1.不设置指定位置,按默认顺序
a='{}{}'.format('hello','duoceshi')
print(a)

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

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

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

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

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

对字典格式化输出
dict1={'name':'duoceshi','class':'dcs8'}
a='{name}{class}'.format(**dict1)
print(a)

dict1={'name':'duoceshi','class':'dcs8'}
# dict2={'age':18}
# a='{0[name]}{0[class]}{1[age]}'.format(dict1,dict2)
# print(a)
#
# zip函数,在接口测试里用的比较多
# list1=['name','class','age','score']
# list2=['duoceshi','dcs8',18]
# 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('D:\多测试\cat.txt','r',encoding='utf-8')
# all=o.readlines()
# print(all)

成都8期马小宇 19:55:27
写的模式    w  writh  有覆盖的作用
# o=open(r'D:\Project\dcs8\lesson\aa.txt','w',encoding='utf-8')
# o.write('123') #在文件中写入字符串,原有的数据会覆盖,写入数据不能为整型
# o.writelines(['1','hello','3'])#列表里面单个元素只能是字符串
# 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()
# ctime()
# sleep(3)
# asctime()
# 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(2)
# print(asctime())
# 第四种导入方法
# from home.home1 import *
# print(num)
# print(list1)
#
# python 中random模块和os板块
#
# radom pytho中的模块常见方法
# import random
# 生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱
# 一组数据等
# 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))#去偶数取不到10
# 5、random.sample(seq,n) #从序列seq中选择n个随机且独立的元素
# list1=[1,21,'test','hello','dcs8']
# print(random.sample(list1,3))
# 6、random.choice(test) #生成随机字符
# str1='hellodcs'
# print(random.choice(str1))
# 7、random.shuffle(list) #洗牌(随机数列)
# list1=[1,21,'test','hello','dcs8']
# 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.exits()判断文件或目录是否存在
# print(os.path.exists(r'D:\Project\dcs8\lesson\aa.txt'))
# 5.os.listdir(dirname)列出指定目录下的文件名或目录名,返回上一个列表
# print(os.path.split(r'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','aa.txt'))
# 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\hello123')
#

【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.#1、math匹配的对象是字符串,匹配出来的结果也是字符串
# 从第一个字符开始匹配,如果第一个字符不是要匹配的类型、则匹配失败并报错#注意:如果规则带了'+',则匹配1次或者多次,无'+'只匹配一次
# 注意:如果规则带了‘+’则匹配1次或者多次,五‘+’值匹配一次
# all= re.math('\d','helloword')   #打印报错\d只能匹配数字,最少匹配数字
# all= re.math('\d','123helloword') #结果为1匹配1次
# all= re.math('\d+','123helloword') #结果为123匹配多次
# all= re.math('\d*','helloword') #不会报错匹配0次或多次
# all= re.math('\d*','123helloword') #结果为123匹配多次
# all= re.math('\d?','helloword') #不会报错匹配0次
# all= re.math('\d?','123helloword')#结果为1匹配1次
# all= re.math('\d{3}','12helloworld')#报错必须匹配3次
# all= re.math('\d{3}','1234helloworld')#结果为123只匹配3次
# all= re.math('\d{1,3}','1helloworld')#结果为1匹配1-3次这里只匹配1次
# all= re.math('\d{1,3}','1234helloworld')#结果为123这里匹配3次
# all= re.math('\d{,3}','1234helloworld')#结果为123匹配3次
# all= re.math('\d{,3}','helloworld')#匹配0次结果为空
# all= re.math('\d{,}','12345helloworld')#结果为12345匹配5次
# all= re.math('\d{1,}','12345helloworld')#结果为12345匹配5次
# all= re.math('\d{3,6}','12helloworld')#匹配报错必须匹配3-6次
# all= re.math('^1\d{1,5}','12345678')#匹配以1开头后面在匹配1-5次结果为123456
# all= re.math('^132\d{2,5}','12345678')#报错匹配以132开头后面在匹配2-5次
# all= re.math('^132\d{2,5}','1324567')#匹配以132开头后面在匹配2-5次结果为1324567
# all = re.match( '^1\D{2,5}','1abc!@#$%' )#\D匹配非数字,结果为1abc!@
# all = re.match( '^\D{2,5}','abc!@#$%')#匹配5次,结果为abc!@
# all = re.match( '\D{2,5}','abc!@?' )#匹配非数字2到5次,但匹配对象为2到5位
# all = re. match('\D{2,5}$','abc!@?')#报错,$符号
# all = re.match( '^1[358][5-9]\d{8}$','13866663333')
分四部分,匹配的字符串必须以1开头,第二位必须是358里的任意一个;
第三位必须在5-9之间任意一一个,第四部分必须有8位。
# all = re.match( '1^1[358] [A-Z]\d{8}$' , '13A66663333')
# all = re.match( '1^1[358][a-z] \d{8}$' , '13a66663333' )
# all = re. match('\D' , '2hello123dcs')#报错,必须以非数字开头
# all = re.match( '\D','hello123dcs')#匹配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' , 'dcshellodcsdcsworlddcs8') #匹配开头和结尾dcs中间的内容 a
# all = re. findall('\d' , 'dcs8hellodcs123') #匹配单个数字字符,结果: ['8', '1','2','3']
# all = re.findall('\d+' , 'dcs8hellodcs123') #多个连续字符匹配,结果为['8','123']
# all = re. findall('\D','ds8hs123')#单个非数字匹配,结果为['d','s','h','s']
# all = re. findall( '\D+','ds8hs123')#多个连续字符匹配,结果为['ds','hs']
# all = re. findall( 'dcs','dcs8hellodcs123') #匹配所有的dcs字符串
# all = re. findall( 'china','dcs8hellodcs123') #匹配不到,返回一个空列表
# 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 个回复

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