主要分为以下两个部分:
1:遍历并读取目录下所有CSV文件,查询固定行数据
2:结果保存到excel文件
#!/usr/bin/env python
# coding=utf-8
import os
import csv
import datetime
import xlsxwriter
import xlrd, xlwt
from xlutils.copy import copy
filePath = r'D:\A123\PSA\test\1PSA\new'
fileList = os.listdir(filePath)
now = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') #当前时间
filename = u'%s/result_%s.xlsx' % (filePath, now) #存放excel的路径
workfile = xlsxwriter.Workbook(filename) # 创建Excel文件,保存
worksheet = workfile.add_worksheet('匹配失败反馈表') # 创建工作表
#format4 = workfile.add_format({'font_size': '12', 'align': 'center', 'valign': 'vcenter', 'bold': True, 'font_color': '#217346', 'bg_color':'#FFD1A4'})
#设置自己想要的标题
worksheet.write(0, 0, "文件名")#, format4) # 设置列的样式
worksheet.write(0, 1, "最小值")#, format4) # 设置列的样式
worksheet.write(0, 2, "最大值")#, format4) # 设置列的样式
line = 1
for file in fileList:
path = os.path.join(filePath, file)
if os.path.isfile(path):
file = open(path, 'r', encoding='utf-8')
print(path)
csvFile = csv.reader(file)
# 读取一行,下面的reader中已经没有该行了
#head_row = next(csvFile)
#print(head_row)
list = []
for i,rows in enumerate(csvFile):
if i == 1033: # 第1030内行
row = rows
for i in range(120,136): # DQ到EF
#print(int(row[i]))
list.append(row[i])
#print(list) # 列表显示从DQ到EF的值
print("最小值=", sorted(list)[0], "最大值=", sorted(list)[15], "\n") # 由小到大排列
# 写入的数据
worksheet.write(line, 0, path)
worksheet.write(line, 1, sorted(list)[0])
worksheet.write(line, 2, sorted(list)[15])
line = line + 1
workfile.close()
参照:
https://blog.csdn.net/weixin_30501857/article/details/97685516?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
https://www.jb51.net/article/180332.htm
https://www.cnblogs.com/Christina-Notebook/p/10867602.html