• 从文件夹中找需要的信息 并生成csv文件


     1 import os,sys
     2 import csv
     3 
     4 file_folder = sys.argv[1]
     5 # file_folder = r"/Users/vito/Desktop/0120 "
     6 final_info={}
     7 txt_path_list=[]
     8 
     9 # 如需要处理更多log信息,在下面三行最后面添加; 如果读不到数据 可以检查关键字是否一致
    10 fileHead = ('distance', 'center_4edge_mean_MTF50_Y','left_diff_MTF50_Y')
    11 request_testItem = ('center_4edge_mean_MTF50_Y','left_diff_MTF50_Y')
    12 distance_list = ["1750", "1755", "1760", "1765", "1770", "1775", "1780", "1785", "1790", "1795", "1800", "1805", "1810", "1815", "1820", "1825", "1830", "1835", "1840", "1845", "1850"]
    13 
    14 def handle_txt(txt_path_list,final_info):
    15     for path in txt_path_list:
    16         with open(path,'r') as f:
    17             content = f.readlines()
    18             log_info = {}
    19             for line in content:
    20                 handled_line_info = line.strip().split(':=') # name:=值
    21                 # print(handled_line_info)
    22                 if handled_line_info[0] in request_testItem:
    23                     log_info[handled_line_info[0]]=handled_line_info[1] # {name:值}
    24             for distance in distance_list:
    25                 if distance in path:
    26                     final_info[distance]=log_info # 字典格式为:distance:{name:值,name:值,name:值}
    27     return final_info
    28 
    29 def update_csv_log(path,final_info):
    30     with open(path,'w') as f:
    31         wr = csv.writer(f, dialect='excel')
    32         wr.writerow(fileHead)
    33         for distance,txt_info in final_info.items():
    34             data_value = []
    35             for item in request_testItem:
    36                 item_value = final_info[distance].get(item)
    37                 data_value.append(item_value)
    38             new_row = (distance,*tuple(data_value))
    39             wr.writerow(new_row)
    40 
    41 def get_txt_path(filefolder,txt_path_list):
    42     tmp_path_List = os.listdir(filefolder)
    43     for File in tmp_path_List:
    44         subPath = os.path.join(filefolder,File)
    45         if os.path.isdir(subPath):
    46             get_txt_path(subPath,txt_path_list)
    47         elif subPath.endswith('txt'):
    48             txt_path_list.append(subPath)
    49             txt_path_list.sort()
    50     # print(txt_path_list)
    51     return txt_path_list
    52 
    53 
    54 txt_path_list = get_txt_path(file_folder,txt_path_list)
    55 final_info = handle_txt(txt_path_list,final_info)
    56 update_csv_log(file_folder+'/WD_summary.csv',final_info)
  • 相关阅读:
    SQL 测试
    atoi的实现
    python基础3 ---python数据类型二
    python基础2 ---python数据类型一
    python基础1 ---python简介
    shell编程3 ---流程控制语句
    shell编程2 ---条件判断语句
    oldboyshell编程扩展内容
    nfs服务器
    shell编程1
  • 原文地址:https://www.cnblogs.com/v113/p/14528060.html
Copyright © 2020-2023  润新知