运用python提取文本信息实战-rdoq time matching

本文介绍了一种从日志文件中批量提取RDOQ运行时间的方法,通过Python脚本实现自动化处理,避免了手动操作的繁琐过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RDOQ Time Matching 实战:
目标:提取rdoq的运行时间。
工程中rdoq运行时间用全局变量统计,编码结束后获得rdoq总的运行时间。
原始做法是将该变量全部连续的写到一个文件里,每次手动填表
python 批处理 结合的尝试 还未完全理解

但是将runtime写到屏幕上,->转入log文件中,直接对log文件的信息做匹配提取,效果也是一样的佳。
要解决的问题:
1、匹配,好简单
2、遍历一个文件夹中的所有文件再做匹配

重点解决的是第二个

pathDir = os.listdir('C:/Users/wangm/Desktop/Bat/RA/log')

将路径下的所有的文件名当作字符串,写在了pathDir中,他是一个list
接下来的操作就可以按照粗暴的list循环的方式做

for filename in pathDir:
        c = ''
        c = 'C:/Users/wangm/Desktop/Bat/RA/log/' + filename
        file_object  = open(c, 'r')
        print(file_object)

        while 1:
            chunk = file_object.readline()
            if chunk == '':
                break
            matchTime = 'RdoqTime'
            time = matching(chunk, matchTime, [1])
            if time:
                write_time(time, filename, "0.00")

                print(time)
                break

另一种结局方式是,为了保证每次读取文件数据的顺序,可以先按照表格顺序建表~
c变量的构成还是粗爆的。

f_out = open('C:/Users/wangm/Desktop/Bat/RA/rdoq.txt', 'w+') 
import re
import string
import os

def matching(chunk, matchsyntax, index):
    if re.match(matchsyntax, chunk):
        splitWord = re.split(r'\s+', chunk)
        matchWord = []
        for i in index:
            matchWord.append(splitWord[i])
        return matchWord

def write_time(time, filename, incline):
    f_out.write("%-10s"%(time[0]))
    f_out.write(" ")
    f_out.write("%40s"%(filename))
    f_out.write("  ")
    f_out.write(incline)
    f_out.write("\n")
try:
    pathDir = os.listdir('C:/Users/wangm/Desktop/Bat/RA/log')
    write_time(["Time"], "FileName", "0.00")#Tag
    for filename in pathDir:
        c = ''
        c = 'C:/Users/wangm/Desktop/Bat/RA/log/' + filename
        file_object  = open(c, 'r')
        print(file_object)

        while 1:
            chunk = file_object.readline()
            if chunk == '':
                break
            matchTime = 'RdoqTime'
            time = matching(chunk, matchTime, [1])
            if time:
                write_time(time, filename, "0.00")

                print(time)
                break
finally:
    file_object.close()
    f_out.close() 


在文本输出格式方面,对其什么的为了方便复制粘贴还是有讲究的
f_out.write("%-10s"%(time[0]))
带格式的写文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值