标签:for append bpa sub 处理 strip file def write
import os,sys
import csv
file_folder = sys.argv[1]
# file_folder = r"/Users/vito/Desktop/0120 "
final_info={}
txt_path_list=[]
# 如需要处理更多log信息,在下面三行最后面添加; 如果读不到数据 可以检查关键字是否一致
fileHead = (‘distance‘, ‘center_4edge_mean_MTF50_Y‘,‘left_diff_MTF50_Y‘)
request_testItem = (‘center_4edge_mean_MTF50_Y‘,‘left_diff_MTF50_Y‘)
distance_list = ["1750", "1755", "1760", "1765", "1770", "1775", "1780", "1785", "1790", "1795", "1800", "1805", "1810", "1815", "1820", "1825", "1830", "1835", "1840", "1845", "1850"]
def handle_txt(txt_path_list,final_info):
for path in txt_path_list:
with open(path,‘r‘) as f:
content = f.readlines()
log_info = {}
for line in content:
handled_line_info = line.strip().split(‘:=‘) # name:=值
# print(handled_line_info)
if handled_line_info[0] in request_testItem:
log_info[handled_line_info[0]]=handled_line_info[1] # {name:值}
for distance in distance_list:
if distance in path:
final_info[distance]=log_info # 字典格式为:distance:{name:值,name:值,name:值}
return final_info
def update_csv_log(path,final_info):
with open(path,‘w‘) as f:
wr = csv.writer(f, dialect=‘excel‘)
wr.writerow(fileHead)
for distance,txt_info in final_info.items():
data_value = []
for item in request_testItem:
item_value = final_info[distance].get(item)
data_value.append(item_value)
new_row = (distance,*tuple(data_value))
wr.writerow(new_row)
def get_txt_path(filefolder,txt_path_list):
tmp_path_List = os.listdir(filefolder)
for File in tmp_path_List:
subPath = os.path.join(filefolder,File)
if os.path.isdir(subPath):
get_txt_path(subPath,txt_path_list)
elif subPath.endswith(‘txt‘):
txt_path_list.append(subPath)
txt_path_list.sort()
# print(txt_path_list)
return txt_path_list
txt_path_list = get_txt_path(file_folder,txt_path_list)
final_info = handle_txt(txt_path_list,final_info)
update_csv_log(file_folder+‘/WD_summary.csv‘,final_info)
标签:for append bpa sub 处理 strip file def write
原文地址:https://www.cnblogs.com/v113/p/14528060.html