python实例:IP,申请数,流量按需降序表格
- List item
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:本实验中IP等信息都是经加工虚构,仅供参考。具体操作如下
提示:以下是本篇文章正文内容,下面案例可供参考
一、python实例:生成一个包含IP,申请数和流量的文本
示例:
二、使用步骤
1.代码
#字典
reqs_file=open(r"D:\xuexi\class\code\week2\ip-reqs.txt","r",encoding="UTF-8")
reqs_list=reqs_file.readline()
reqs_list=reqs_file.readlines()
flow_file=open(r"D:\xuexi\class\code\week2\ip-flow.txt","r",encoding="UTF-8")
flow_list=flow_file.readline()
flow_list=flow_file.readlines()
reqs_dict={}
for line in reqs_list:
x=line.split()
reqs_dict[x[0]]=x[1]
flow_dict = {}
for line in flow_list:
x = line.split()
flow_dict[x[0]] = x[1]
reqs_sor=sorted(reqs_dict.items(),key=lambda x:int(x[1]),reverse=True)#降序
result_reqs=[]
for x in reqs_sor:
ip=x[0]
res="\t".join([ip,reqs_dict[ip],flow_dict[ip]])
res+="\n"
result_reqs.append(res)
list1=[]
list2=[]
list3=[]
for x in flow_dict.items():
if x[1][-2:]=="GB":
list1.append(x)
elif x[1][-2:]=="MB":
list2.append(x)
else:
list3.append(x)
list1=sorted(list1,key=lambda x:float(x[1][:-2]),reverse=True)
list2=sorted(list2,key=lambda x:float(x[1][:-2]),reverse=True)
list3=sorted(list3,key=lambda x:float(x[1][:-2]),reverse=True)
flow_sor=list1+list2+list3
result_flow=[]
for x in flow_sor:
ip=x[0]
res="\t".join([ip,reqs_dict[ip],flow_dict[ip]])
res+="\n"
result_flow.append(res)
f1=open(r"D:\xuexi\class\code\week2\mine-ip-reqs.txt","w")
f1.writelines(result_reqs)
f2=open(r"D:\xuexi\class\code\week2\mine-ip-flow.txt","w")
f2.writelines(result_flow)
reqs_file.close()
flow_file.close()
f1.close()
f2.close()
print("done!!!")
总结
这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅举例了如何制作类如网页中排序的文本,旨在熟练运用字典,元组,列表的知识