python读取txt文件内容并进行分析
今天和大家分享一下python如何读取txt文件内容并进行数据分析的需求:某txt文件中存在很多数据,这些数据的某一个属性主要分为A、B、C、D四类,要求把数据分成四类求某一数值属性的平均值,并统计生成超过平均值两倍或者三倍的数据。打包成exe应用程序。...
·
今天和大家分享一下python如何读取txt文件内容并进行数据分析的
需求:某txt文件中存在很多数据,这些数据的某一个属性主要分为A、B、C、D四类,要求把数据分成四类求某一数值属性的平均值,并统计生成超过平均值两倍或者三倍的数据。打包成exe应用程序。
目录
一、实现思路
1、读取文件,获取文件的每一行数据
2、分析文件数据,返回其独特的分割方法(这里我的txt文件是以制表符分割)
3、因为readlines读出来之后是列表形式,所以我们可以利用索引取我们想要的数据
4、根据需求设定判断条件
5、满足条件的数据重新写入某一个。txt文件里面
6、要平均值我们就加平均值
7、要打印我们就加打印
8、还要统计报表我们就根据需求再创建.txt文件进行追加
9、加入我们的input,try except进行优化
10、pyinstaller打包
11、成功
二、代码展示
"""
调度统计
是需要把算法文件中的PassiveReplan ActiveReplan DeadLockReplan SolDeadLock 数据分为4类,
再分别将这4类的perTskNum求平均值,再生成超过平均值x倍的数据,合成一个统计报表,最后剔除查过平均值x倍的数据求平均值。
"""
import time
import os
PassiveReplan = 0
PassiveReplan_all = 0
ActiveReplan = 0
ActiveReplan_all = 0
DeadLockReplan = 0
DeadLockReplan_all = 0
SolDeadLock = 0
SolDeadLock_all = 0
filename = input("请上传文件:")
time_now = time.strftime("%Y-%m-%d-%H-%M-%S")
print("\n\n")
# 判断是否存在Scheduling_document文件夹没有的话新增
if os.path.exists("./Scheduling_document"):
pass
else:
# 创建文件夹
os.mkdir("./Scheduling_document")
# 读取文件
try:
with open(filename,"r") as f:
data = f.readlines()
for words in data:
word = words.split("\t")
if word[1] == "PassiveReplan":
with open(f"./Scheduling_document/{time_now}-PassiveReplan.txt", "a", encoding="utf-8") as f:
f.write(str(word))
f.write("\n")
PassiveReplan_all += eval(word[4])
PassiveReplan += 1
PassiveReplan_agv = PassiveReplan_all / PassiveReplan
if word[1] == "ActiveReplan":
with open(f"./Scheduling_document/{time_now}-ActiveReplan.txt","a",encoding="utf-8") as f:
f.write(str(word))
f.write("\n")
ActiveReplan_all += eval(word[4])
ActiveReplan += 1
ActiveReplan_agv = ActiveReplan_all / ActiveReplan
if word[1] == "DeadLockReplan":
with open(f"./Scheduling_document/{time_now}-DeadLockReplan.txt", "a", encoding="utf-8") as f:
f.write(str(word))
f.write("\n")
DeadLockReplan_all += eval(word[4])
DeadLockReplan += 1
DeadLockReplan_agv = DeadLockReplan_all / DeadLockReplan
if word[1] == "SolDeadLock":
with open(f"./Scheduling_document/{time_now}-SolDeadLock.txt", "a", encoding="utf-8") as f:
f.write(str(word))
f.write("\n")
SolDeadLock_all += eval(word[4])
SolDeadLock += 1
SolDeadLock_agv = SolDeadLock_all / SolDeadLock
except Exception as e:
print("没有这个文件",e)
# 追加内容
try:
with open(f"./Scheduling_document/{time_now}-PassiveReplan.txt", "a", encoding="utf-8") as f:
f.write("PassiveReplans类的平均值为:")
f.write(str(PassiveReplan_agv))
f.write(f"\n'生成报告时间{time_now}'")
with open(f"./Scheduling_document/{time_now}-ActiveReplan.txt", "a", encoding="utf-8") as f:
f.write("ActiveReplan类的平均值为:")
f.write(str(ActiveReplan_agv))
f.write(f"\n'生成报告时间{time_now}'")
with open(f"./Scheduling_document/{time_now}-DeadLockReplan.txt", "a", encoding="utf-8") as f:
f.write("DeadLockReplan类的平均值为:")
f.write(str(DeadLockReplan_agv))
f.write(f"\n'生成报告时间{time_now}'")
with open(f"./Scheduling_document/{time_now}-SolDeadLock.txt", "a", encoding="utf-8") as f:
f.write("SolDeadLock类的平均值为:")
f.write(str(SolDeadLock_agv))
f.write(f"\n'生成报告时间{time_now}'")
except Exception as e:
print(f"{filename}中没有PassiveReplan/ActiveReplan/DeadLockReplan/SolDeadLock分类",e)
try:
print(f"PassiveReplan类的平均值为:{PassiveReplan_agv},报告时间:{time_now}")
print(f"ActiveReplan类的平均值为:{ActiveReplan_agv},报告时间{time_now}")
print(f"DeadLockReplan类的平均值为:{DeadLockReplan_agv},报告时间{time_now}")
print(f"SolDeadLock类的平均值为:{SolDeadLock_agv},报告时间{time_now}")
except Exception as e:
print("无法找到xxx_agv平均值",e)
print("\n\n")
print("---------生成指定报告----------\n"
"说明:参考标准,各分类平均值,需确定倍数multiple\n"
"效果:将生成大于multiple倍的数据\n"
"报告位置:dict目录下的Scheduling_document")
print("\n")
multiple = input("请输入倍数:")
with open(filename, "r") as f:
data = f.readlines()
for words in data:
word = words.split("\t")
if word[1] == "PassiveReplan":
with open(f"./Scheduling_document/{multiple}倍-PassiveReplan.txt", "a", encoding="utf-8") as f:
if float(word[4]) > PassiveReplan_agv * eval(multiple):
f.write(str(word))
f.write("\n")
if word[1] == "ActiveReplan":
with open(f"./Scheduling_document/{multiple}倍-ActiveReplan.txt", "a", encoding="utf-8") as f:
if float(word[4]) > ActiveReplan_agv * eval(multiple):
f.write(str(word))
f.write("\n")
if word[1] == "DeadLockReplan":
with open(f"./Scheduling_document/{multiple}倍-DeadLockReplan.txt", "a", encoding="utf-8") as f:
if float(word[4]) > DeadLockReplan_agv * eval(multiple):
f.write(str(word))
f.write("\n")
if word[1] == "SolDeadLock":
with open(f"./Scheduling_document/{multiple}倍-SolDeadLock.txt", "a", encoding="utf-8") as f:
if float(word[4]) > SolDeadLock_agv * eval(multiple):
f.write(str(word))
f.write("\n")
print("请前往dist目录下的Scheduling_document查看")
exite = input("按任意键退出:")
三、总结
此代码最为练手使用,其中运用了文件读写,数据类型的转换、数据索引等知识点。最后为了方便使用打包成.exe应用程序 打包命令:
pyinstall -F XXX.py
感谢大家支持!!!
更多推荐
已为社区贡献1条内容
所有评论(0)