python学习日记2-3周
怠惰怠惰
improt 导入
import phone.apple.iphone6
import phone.apple.iphone7
import phone.samsung.note.galaxy_note8
from phone.samsung.s.galaxy_s7 import askPrice as ask
print('start test1')
phone.apple.iphone6.askPrice()
phone.apple.iphone7.askPrice()
phone.samsung.note.galaxy_note8.askPrice()
ask()
问题:从不同目录下导入模块
代码:
1.没啥特别逻辑
2.同目录下的不同文件,必须单独写两行,不能一个语句导入俩文件
3.使用导入模块下方法时,需要将导入路径写出来
4.from xxx import 导入,使用方法时不需要重复写路径,as可重命名
设置sys路径
import imp
import sys
sys.path.append(r'C:\Users\Administrator\Desktop\Test\BYHY\test6_import\phone')
from phone.apple import iphone6,iphone7
from phone.samsung.note import galaxy_note8
from phone.samsung.s import galaxy_s7
for path in sys.path:
print (path)
print('start test')
iphone6.askPrice()
iphone7.askPrice()
galaxy_note8.askPrice()
galaxy_s7.askPrice()
问题:设置sys路径,实现一个目录导入多个文件
啥是sys?
以下为白月黑羽的教材说明。
这里是引用
http://v3.byhy.net/tut/py/basic/14/#syspath
代码:
1.通过sys.path.append将指定路径加入sys目录
2.加入代码要先执行一次,不然后续代码还是会找不到
3.未加入sys的目录,导入时,from后必须指定到一个文件,
4.加入sys目录后,导入时,from后可为一个文件夹路径,并且导如该文件夹内的多个文件
字典1
def find_start_end(file_text:str, math_text1: str,math_text2:str): #定义一个取两个字段中间字段的方法
text1_end = file_text.find(math_text1)+len(math_text1) #根据第一个字段和其长度找到中间字段的开始索引
if math_text2 == '': #增加判断条件,当第二个参数填入‘’,认定为是到字符串的末尾
text2_start = len(file_text) #由于第二个字段是空,所以中间字段的结束值就是字符串的最后
else:
text2_start = file_text.find(math_text2)#根据第二个字段的开始索引找到中间字段的结束索引
between_text = file_text[text1_end:text2_start] #根据中间字段的起止索引得出中间字段
return between_text
with open('2019-10-22_11.05.40.log', 'r', encoding='utf8') as test7file:
file_read = test7file.readlines()
count_dic = {
} #定义一个空字典拿来装归类好的数据
overtime = 0 #定义一个0的变量来统计超时的数量
for num in range(len(file_read)): #循环传入每一行日志
if file_read[num].find('list_order >') == -1: #判断是否超时,找不到对应字段则认为超时
overtime += 1 #发现一个超时数量就加1
count_dic['响应超时'] = overtime #将超时作为一个key,数量作为value进行统计
else:
key = find_start_end(file_read[num],'list_order >','s|sales_') #找到时间信息并作为一个key记录
vaule = find_start_end(file_read[num],'s|sales_','\n') #找到数量信息,并作为value记录
if key not in count_dic: #判断是否已经存在了一个同名key(同一时间信息),
count_dic[key] = vaule #不存在则作为一个新的key把时间信息和数量信息写入
else: #如果已经存在了名key,则把数量信息的value进行相加后写入
count_dic[key] = int(count_dic[key])+int(vaule)
for k,v in count_dic.items(): #循环输出key和value信息,
if k != '响应超时' : #根据是否是响应超时来决定输出的文案内容
print(f'API list_order >{
k}s : {
v}个')
else :
print(f'{
k},{
v}个')
print(count_dic.items())
问题:从日志文件中,统计指定信息出现次数
代码:
1.写了find_start_end 方法,来提取两个字符串中间的值,
2.老实说这个方法有点多余,日志文件其实有明确的格式区分,
3.当然,这个方法还是能用的
4.整体没啥特别逻辑,无非就是提取内容,进行统计。
字典2
#定义一个通过key查询的方法
def select_stock_key(select_dic1