首页 > 编程 > Python > 正文

python linecache 处理固定格式文本数据的方法

2019-11-25 13:31:11
字体:
来源:转载
供稿:网友

小程序大功能

对一批报文要处理要处理里面的得分,发现python linecache ,特记录如下。

#!/usr/bin/env python# -*- coding: utf-8 -*-''''提取result结果分值''' import linecacheimport osimport os.path rootdir = r"./815old/" for parent, dirnames, filenames in os.walk(rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字  for filename in filenames: # 输出文件信息    files = os.path.join(parent, filename) # 输出文件路径信息    count = len(open(files, 'r').readlines()) # 行数    str = linecache.getlines(files) # 所有文件的字符串    filename = str[2] #第三行    if count > 6: # 行数大于5行表示命中      matchRatio = str[8] #第九行      print filename[58:-2], " ", matchRatio[15:], "/r"    else: # 没有命中 matchRatio 赋值0      print filename[58:-2], " ", '0', "/r"

文本格式如下:

results { fileID: "386e86d2e3991de32a028b7e26e58ffd" filename: "/opt/vfpgen/video_file/192.168.142.199/video/Avatar.2009.iTALiAN.READNFO.LD.TS.XviD-SiLENT.CD1[S.o.M.].avi" filetype: 1 vfpQueryResults {  fileID: "09b509f6c625aeb525f6f1c8e09725d6"  filename: "/opt/SmartVision/system/copyrightfpdb//dbfp/09b509f6c625aeb525f6f1c8e09725d6.DB.cdvs"  matchScore: 0.85874754  matchRatio: 0.53333336  dbID: "copyright_library_db"  startframe: 16210  endframe: 97240 }}

以上这篇python linecache 处理固定格式文本数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表