找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
# python 当中的内置函数
# {[]}.format()
# {}里面为format后边括号内字符串/列表/字典的索引号
# []里面为对应索引的符串/列表/字典内的元素的序列号/序列号/键名

# a='{0[1]}{1[2]}'.format('asdfg','zxcv')
# print(a)        # sc

# a='{}{}'.format('asdfg','zxcv')       #三个{}会报错
# print(a)        # asdfgzxcv

# a='{0} {1} {0}'.format('asdfg','zxcv')    #'{}'为一个字符串,之间可以添加任何字符
# print(a)        # asdfg zxcv asdfg

# a='{xxx}{name}{xxx}'.format(name='asdfg',xxx='zxcv')
# print(a)        #zxcvasdfgzxcv

# list1=['asdf','125',333]
# a='{0}{0}'.format(list1)
# print(a)        # ['asdf', '125', 333]['asdf', '125', 333]

# list1=['asdf','125',333]
# list2=['qwer']
# a='嘿嘿 {0[1]}{1[0]}'.format(list1,list2)
# print(a)        # 嘿嘿 125qwer

# list1=['asdf','125',333]
# list2=['qwer']
# a='{0[1]}{1}'.format(list1,list2)
# print(a)        # 125['qwer']

# dict1={'name':'hhh','age':'12'}
# a='{name} {age}'.format(**dict1)
# print(a)        # hhh 12

# dict1={'name':'hhh','age':'12'}
# a='{0[name]} {0[age]}'.format(dict1)
# print(a)        # hhh 12

# dict1={'name':'hhh','age':'12'}
# a='{name} {age}'.format(dict1)
# print(a)        # 错误,无法输出

# list1=['asdf','125',333]
# dict1={'name':'hhh','age':'12'}
# a='{0[1]} {1[age]}'.format(list,dict1)
# print(a)        # 错误,无法输出

#zip 函数,在接口测试里面用的较多
list1=['name','class','age','score']
list2=['dcs','dcs8',18]
a=zip(list1,list2)      #以最短列为主
# print(a)        # <zip object at 0x000001D204D410C8>

# print(list(a))  # [('name', 'dcs'), ('class', 'dcs8'), ('age', 18)]
#列表里面为元组
# print(dict(a))  # {'name': 'dcs', 'class': 'dcs8', 'age': 18}
#三个键对值

# open 函数
# open()函数用于打开一个文件,创建一个file文件
# 语法:open(file,mode),模式有r(只读),w(写入覆盖),a(写入追加)
#读的模式r  read

# o=open(r'D:\PycharmProjects\duoceshi\lesson\aa','r',encoding='utf-8')
# 文件路径前边加r  阻止\a转义
# encoding='utf-8'  文件或是路径中含有中文,为了防止乱码
# a=o.read()  #读取文件所有内容,返回字符串
# a=o.readlines()   #读取文件所有内容,返回列表,\n表示换行
# a=o.readline()    #读取文件第一行内容,返回字符串类型
# print(a)

# o=open(r'D:\PycharmProjects\duoceshi\lesson\aa','w',encoding='utf-8')
# o.write('123')  #写入字符串,原有数据会被覆盖,写入数据不能为整型
# o.writelines(['1','hhh','2'])   #列表里面单个元素只能是字符串
# o.close()   #关闭文件

# o=open(r'D:\PycharmProjects\duoceshi\lesson\aa','a',encoding='utf-8')
# o.write('123')  #写入字符串,原有数据不会覆盖,写入数据不能为整型
# o.writelines('\n123456')    #追加之前先换行
# o.writelines('中国\n')    #追加之后换行
# o.close()

# with open(r'D:\PycharmProjects\duoceshi\lesson\aa','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)

#
#pythonrandom模块和os模块】
# python中的random模块
# 生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等
# random模块中常见的方法:
# 1random.random() #该方法是生成0-1之间的浮点数,但是能取到0,但是取不到1
# print(random.random())

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


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

# 5random.sample(seq,n) #从序列seq中选择n个随机且独立的元素
# list1=[1,2,'kkk','ggg','aaa']
# print(random.sample(list1,3))


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

# 7random.shuffle(list) #洗牌(随机数列)
# list1=[1,2,'kkk','ggg','aaa']
# random.shuffle(list1)
# print(list1)


# 8hashlib.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())

# os.path.isfile()  #判断是否为文件
# print(os.path.isfile('D:\PycharmProjects\duoceshi\lesson\lesson2.py'))

# os.path.isdir()   #判断是否为目录
# print(os.path.isdir('D:\PycharmProjects\duoceshi\lesson'))

# os.path.exixts()  #判断是否为文件或目录
# print(os.path.exists('D:\PycharmProjects\duoceshi\lesson\lesson2.py'))


# os.listdir(dirname)   # 列出指定目录下的文件和目录名,返回一个列表
# print(os.listdir('D:\PycharmProjects\duoceshi\lesson'))


# os.path.split(name)   # 分割一个完整的路径的最后一个文件或目录
# print(os.path.split('D:\PycharmProjects\duoceshi\lesson'))

# # os.path.join(path,name)   # 连接目录与文件或目录
# print(os.path.join('D:\PycharmProjects\duoceshi\lesson','aa.txt'))


# os.mkdir(dir)
# print(os.mkdir('D:\PycharmProjects\duoceshi\lesson\hollo'))


# os.rename(old,new)
# print(os.rename('D:\PycharmProjects\duoceshi\lesson\hollo','D:\PycharmProjects\duoceshi\lesson\hhh'))

# import re
# 1match
# 匹配的对象是字符串,匹配出来的结果也是字符串
#从第一个字符开始匹配,如果第- -个字符不是要匹配的类型、则匹配失败并报错
# 注意:如果规则带了'+',则匹配1次或者多次,无'+'只匹配一次
# all = re. match('\d','helloworld') #打印报错,\d只能匹配数字,至少匹配1
# all = re.match('\d','123helloworld' )#结果为1,匹配1
# all = re.match('\d+','123helloworld' )#结果为123,匹配多次
# all = re.match('\d*','helloworld' )#不会报错,匹配0次或多次
# all = re.match('\d*','123helloworld' )#结果为123,匹配多次
# all = re. match('\d?','helloworld') #不会报错,匹配0
# all = re.match('\d?',' 123helloworld')#结果为1,匹配1
# all = re.match('\d{3}',' 12helloworld ' )#报错,必须匹配3
# all = re.match('\d{3}','1234helloworld')#结果为123,只匹配3
# all = re.match('\d{1,3}', 1helloworld' )#结果为1,匹配13次,这里只匹配1
# all = re.match('\d{1,3}' ,' 1234helloworld' )#结果为123,这里匹配3
# all = re. match('\d{ ,3}','1234helloworld' )#结果为123,匹配3
# all = re. match('\d{ ,3}','helloworld')#匹配0次,结果为空
# all = re.match('\d{,}','12345helloworld )#结果为12345,匹配5
# all = re. match('\d{1,}',' 12345helloworld' )#结果为12345,匹配5
# all = re. match( '\d{3,6}','12helloworld' )#匹配报错,必须要匹配36
# all = re.match('^1\d{1,5}','123456781)#匹配以1开头,后面再匹配15次,结果为123456
# all = re.match( 1^132\d{2, 5},'12345678')#报错,匹配以132开头,后面再匹配25
# all = re.match( '^132\d{2,5}','1324567')#匹配以132开头,后面再匹配25次,结果1324567
# all = re.match( 1^1\D{2,5}','1abc !@#$%' )#\D匹配非数字,结果为1abc!@
# all = re.match( I^\D{2,5}','abc!@#$%')#匹配5次,结果为abc!@
# all = re.match( '\D{2,5}','abc!@?' )#匹配非数字25次,但匹配对象为25
# all = re. match('\D{2,5}$','abc!@?')#报错,$符号
# all = re.match( I^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() )
#2search
#从第一个字符开始查找、一找到就返回第- -个字符串,找到就不往下找,找不到则报错
# 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')
#匹配连续非数字25次,结果为he
# all = re.search('\D{2,5}','h321duoceshi') #匹配结果为duoce
# print(all.group())

# 3findall
#从第一个字符开始查找,找到全部相关匹配为止,找不到返回一个列表[]
# all = re. findall('dcs(.+)dcs','dcshellodcs8') #匹配括号里面的内容,两个dcs中间的内容
# all = re. findall('dcs(. +)dcs','dcshellodcsdcsworlddcs8') #匹配开头和结尾dcs中间的内容
# 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))

# 4compile
# 编译模式生成对象,找到全部相关匹配为止,找不到返回一个列表[]
# all = re. compile('hello')
#先编译
# a = all. findall('dcshello123test')
#结果为[ 'hello']
# a = all. findall('dcs123test')
#[]
# print(a)
# 5string模块
# import string
# num1 = string.digits#获取0-9的数字
# num2 = string.ascii_letters#获取大小写所有字母
# print (num1, type (num1) )
# print ( num2, type (num2))
分享至 : QQ空间
收藏

0 个回复

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