python读csv文件写txt文件
2022/5/4 14:12:59
本文主要是介绍python读csv文件写txt文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# -*- coding:UTF-8 -*- import os import io import os.path import shutil import csv import numpy as np def writefile_title(path,reportdate): reportfile = path + '\\' + reportdate file=open(reportfile,'w') def findfile(path_report,summary): i=0 file_dir=[] for root, dirs, files in os.walk(path_report): for file_name in files: if summary in file_name: result=os.path.join(root, file_name).replace('\\', '/') i += 1 file_dir.append(result) print(file_dir) return file_dir def writefile_content(file_dir,reportdate): l = len(file_dir) reportfile = path + '/' + reportdate #f_2 = io.open(str(reportfile), "w") lable=[] total_date=[] cpu_max=[] disk_max=[] Memory_max=[] Network_max=[] for i in range(0,l): cpu_date = [] disk_date=[] Memory_date=[] Network_date=[] file=file_dir[i] array_1 = file.split('/') lable.append(array_1) l2=len(lable) lable_name=[] for k in range(0,l2): h=lable[k] lable_name.append(h[-2]) with open(file,'rt') as csvfile: reader = csv.reader(csvfile) rows_cpu = [row[1] for row in reader] with open(file,'rt') as csvfile: reader = csv.reader(csvfile) rows_disk=[row[2] for row in reader] with open(file,'rt') as csvfile: reader = csv.reader(csvfile) rows_Memory=[row[3] for row in reader] with open(file,'rt') as csvfile: reader = csv.reader(csvfile) rows_Network=[row[4] for row in reader] rows_cpu=rows_cpu[1:] l_cpu = len(rows_cpu) temp_cpu=float(0) rows_disk = rows_disk[1:] temp_disk = float(0) rows_Memory = rows_Memory[1:] temp_Memory = float(0) rows_Network = rows_Network[1:] temp_Network = float(0) for i in range(0,l_cpu): date_cpu_str=rows_cpu[i] date_cpu_int=float(date_cpu_str) cpu_date.append(date_cpu_int) date_disk_str = rows_disk[i] date_disk_int = float(date_disk_str) disk_date.append(date_disk_int) date_Memory_str = rows_Memory[i] date_Memory_int = float(date_Memory_str) Memory_date.append(date_Memory_int) date_Network_str = rows_Network[i] date_Network_int = float(date_Network_str) Network_date.append(date_Network_int) if cpu_date[i]>=temp_cpu: temp_cpu=cpu_date[i] else: temp_cpu=temp_cpu if disk_date[i] <= temp_disk: temp_disk = disk_date[i] else: temp_disk = temp_disk if Memory_date[i]>=temp_Memory: temp_Memory=Memory_date[i] else: temp_Memory=temp_Memory if Network_date[i]>=temp_Network: temp_Network=Network_date[i] else: temp_Network=temp_Network cpu_max.append(temp_cpu) disk_max.append(temp_disk) Memory_max.append(temp_Memory) Network_max.append(temp_Network) # print(cpu_max) # print(disk_max) # print(Memory_max) # print(Network_max) # print(lable) total_date.append(lable_name) total_date.append(cpu_max) total_date.append(disk_max) total_date.append(Memory_max) total_date.append(Network_max) print(total_date) t='' total_date = np.array(total_date) row = total_date.shape[0] col = total_date.shape[1] with open(reportfile, 'w') as q: q.write('Lable,CPU,Disks I/O,Memory,Network I/O\n') for i in range(0,col): for j in range(0,row): t = t + str(total_date[j][i]) + ',' q.write(t[0:len(t)-1]) q.write('\n') t = '' if __name__ == '__main__': summary='PerfMon-Metrics-Collector.csv' reportdate='reportdate.txt' path_report=r'D:\WorkSpace\apache-jmeter-3.1\TestReport' path=r'D:\WorkSpace\apache-jmeter-3.1\TestReport' old_file=path+'\\'+summary writefile_title(path, reportdate) file_dir=findfile(path_report, summary) writefile_content(file_dir, reportdate)
这篇关于python读csv文件写txt文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南