|
Python自动化来参与一下:
# @Version: 1.0
# @Author : 汤灏
# @File : testPandas1.1.py
# @Time : 2024/2/2 16:29
import os,re,openpyxl
import pandas as pd
dir=r'D:\PythonProject\pythonLearning\ExcelHome\从txt文件夹中找出有问题的unit的数据\Camera Test data log\all'
df_list=[]
for file in os.listdir(dir):
if file.endswith('.txt'):
with open((full_path:=os.path.join(dir,file)),'r',encoding='utf-8') as f:
content=f.read()
pat_serial_num = re.compile(r'\b\w{14}\b')
serial_num = pat_serial_num.findall(content)
print(serial_num[0])
pat_date = re.compile(r'Date:\s+([A-Z].+2024)\b')
dateExtract = pat_date.findall(content)
print(dateExtract[0])
pat_time = re.compile(r'\d:\d{2}:\d{2} [AP]M')
timeExtract = pat_time.findall(content)
print(timeExtract[0])
pat_failed = re.compile(r'\b(\d.+?):\s+Failed')
failedExtract = pat_failed.findall(content)
print(failedExtract)
# sdf=pd.DataFrame([
# {
# "Serial number":serial_num,
# "Date":dateExtract,
# "Time":timeExtract,
# "Unit":failedExtract,
# "Failed":['failed']*len(failedExtract)
# }]
# )
# print(sdf)
s1 = pd.Series(serial_num[0], name='S')
s2 = pd.Series(dateExtract, name='D')
s3 = pd.Series(timeExtract, name='T')
s4 = pd.Series(failedExtract, name='U')
s5=pd.Series(['失败']*len(failedExtract),name='Failed')
if failedExtract:
sdf = pd.DataFrame([s1, s2, s3, s4,s5]).T.reindex(range(len(failedExtract)+1))
df_list.append(sdf)
fdf=pd.concat(df_list)
print(fdf)
fdf.to_excel('sss1.xlsx',index=False)
|
|