1.读取指定路径下的csv文件,获取内容制成matplotlib图
#_*_coding:utf8_*_
import os
# import matplotlib as mpl
import matplotlib.pylab as plt
import re
from matplotlib.pyplot import MultipleLocator
def getfile(path):
file_tpye = '.csv'
file_pro= 'TecFineTune'
paths = os.walk(path)
for root,dirs,files in paths:
for file in files:
if (file_tpye in file) and (file_pro in file):
return str(os.path.join(root,file))
give_Path =r'/Users/prm2160/Desktop/Double/PRM_BC7'
def aa(path):
commed_list = []
file = open(path, "r") # 打开文件
file.readline() # 读取文件内容
allContect = file.read()
allList = re.findall(r'(.+),(.+),(.+),(.+),(.+),.+,.+,.+,.+,.+,.+,.+', allContect)
# print(allList)
allDict = {}
file.close()
for x in allList:
if x[2] != "NA" and x[3] != "NA" and x[4] != "NA":
if x[1] not in allDict:
allDict[x[1]]=[]
allDict[x[1]].append([x[0], float(x[2]), float(x[3]), float(x[4])])
timeStamp1 = []
dut_target_temp = []
dut_temp = []
ntc_temp = []
for i in allDict[x[1]]:
timeStamp1.append(i[0])
dut_target_temp.append(i[1])
dut_temp.append(i[2])
ntc_temp.append(i[3])
for i in allDict.keys():
y_major_locator = MultipleLocator(3)
ax = plt.gca()
ax.yaxis.set_major_locator(y_major_locator)
plt.tick_params(axis='both', labelsize=6)
plt.title(i)
plt.plot(timeStamp1, dut_target_temp, linewidth=1, linestyle='', marker='o', label='dut_target_temp')
plt.plot(timeStamp1, dut_temp, linewidth=1, linestyle='', marker='o', label='ntc_temp')
plt.plot(timeStamp1, ntc_temp, linewidth=1, linestyle='', marker='o', label='dut_temp')
plt.legend(loc='lower right') # 将图例放置右下角
plt.show()
# plt.savefig(give_Path+'/'+i+".png")
# plt.savefig(c+"_"+b+".png")
plt.close()
aa(getfile(give_Path))
timeStamp dut_cmd dut_target_temp dut_read_new sensor1 sensor2 sensor3 sensor4 clvr_temp toohot_count tec_will_set peak_temp
08/18/2022 14:18:58.985 SET-FIXTURE-TECTEMP-SRAM 85 NA 29.4 29.4 29.4 29.4 NA NA NA NA
08/18/2022 14:18:59.828 SET-FIXTURE-TECTEMP-SRAM 85 NA 29.4 29.4 29.5 29.5 NA NA NA NA
08/18/2022 14:19:00.682 SET-FIXTURE-TECTEMP-SRAM 85 NA 29.5 29.5 29.5 29.5 NA NA NA NA
08/18/2022 14:19:02.839 SET-FIXTURE-TECTEMP-SRAM 85 NA 29.1 29.1 28.9 28.9 NA NA NA NA
08/18/2022 14:19:04.160 SET-FIXTURE-TECTEMP-SRAM 85 NA 28.4 28.4 28.200001 28.200001 NA NA NA NA
08/18/2022 14:19:05.678 SET-FIXTURE-TECTEMP-SRAM 85 NA 27.5 27.5 27.5 27.200001 NA NA NA NA
08/18/2022 14:19:07.000 SET-FIXTURE-TECTEMP-SRAM 85 NA 26.700001 26.5 26.5 26.5 NA NA NA NA
08/18/2022 14:19:08.441 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.700001 25.700001 25.5 25.5 NA NA NA NA
08/18/2022 14:19:09.839 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.1 24.9 24.9 24.700001 NA NA NA NA
08/18/2022 14:19:11.167 SET-FIXTURE-TECTEMP-SRAM 85 NA 24.4 24.4 24.4 24.200001 NA NA NA NA
08/18/2022 14:19:12.681 SET-FIXTURE-TECTEMP-SRAM 85 NA 23.9 23.799999 23.799999 23.799999 NA NA NA NA
08/18/2022 14:19:14.011 SET-FIXTURE-TECTEMP-SRAM 85 NA 23.6 23.6 23.6 23.6 NA NA NA NA
08/18/2022 14:19:15.479 SET-FIXTURE-TECTEMP-SRAM 85 NA 23.6 23.6 23.6 23.700001 NA NA NA NA
08/18/2022 14:19:16.845 SET-FIXTURE-TECTEMP-SRAM 85 NA 23.700001 23.700001 23.700001 23.700001 NA NA NA NA
08/18/2022 14:19:18.137 SET-FIXTURE-TECTEMP-SRAM 85 NA 23.9 23.9 23.9 23.9 NA NA NA NA
08/18/2022 14:19:19.451 SET-FIXTURE-TECTEMP-SRAM 85 NA 24.1 24.1 24.1 24.200001 NA NA NA NA
08/18/2022 14:19:20.831 SET-FIXTURE-TECTEMP-SRAM 85 NA 24.4 24.4 24.4 24.4 NA NA NA NA
08/18/2022 14:19:22.145 SET-FIXTURE-TECTEMP-SRAM 85 NA 24.6 24.700001 24.700001 24.799999 NA NA NA NA
08/18/2022 14:19:23.428 SET-FIXTURE-TECTEMP-SRAM 85 NA 24.9 24.9 24.9 25 NA NA NA NA
08/18/2022 14:19:24.826 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.1 25.1 25.1 25.1 NA NA NA NA
08/18/2022 14:19:26.147 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.200001 25.299999 25.299999 25.299999 NA NA NA NA
08/18/2022 14:19:27.431 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.299999 25.299999 25.299999 25.4 NA NA NA NA
08/18/2022 14:19:28.850 SET-FIXTURE-TECTEMP-SRAM 85 NA 25.4 25.4 25.4 25.4 NA NA NA NA
2.使用pyqt实现
import sys
import os
import re
import matplotlib.pylab as plt
from matplotlib.pyplot import MultipleLocator
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.paths = ""
self.setWindowTitle('文件拖入') # ==> 窗口标题
self.resize(500, 400) # ==> 定义窗口大小
ico_file = "../qt/ico/Ferrari.ico"
self.setWindowIcon(QIcon(ico_file))
self.textBrowser = QTextBrowser()
self.setCentralWidget(self.textBrowser)
self.setAcceptDrops(True) # ==> 必须设置
self.btn = QPushButton("清空", self)
self.btn.setGeometry(200, 300, 100, 30)
self.btn.clicked.connect(self.when_btn_click)
def when_btn_click(self):
self.textBrowser.setText("")
def dragEnterEvent(self, event):
file = event.mimeData().urls()[0].toLocalFile()
if file not in self.paths: # ==> 去重显示
self.paths += file + "\n"
self.textBrowser.setText(self.paths)
a = str(file)
print(a)
file_tpye = '.csv'
file_pro = 'TecFineTune'
paths = os.walk(a)
for root, dirs, files in paths:
for file in files:
if (file_tpye in file) and (file_pro in file):
pathes = os.path.join(root, file)
path1 = str(pathes)
file = open(path1, "r") # 打开文件
file.readline() # 读取文件内容
allContect = file.read()
allList = re.findall(r'(.+),(.+),(.+),(.+),(.+),.+,.+,.+,.+,.+,.+,.+', allContect)
# print(allList)
allDict = {}
file.close()
for x in allList:
if x[2] != "NA" and x[3] != "NA" and x[4] != "NA":
if x[1] not in allDict:
allDict[x[1]] = []
allDict[x[1]].append([x[0], float(x[2]), float(x[3]), float(x[4])])
timeStamp1 = []
dut_target_temp = []
dut_temp = []
ntc_temp = []
for i in allDict[x[1]]:
timeStamp1.append(i[0])
dut_target_temp.append(i[1])
dut_temp.append(i[2])
ntc_temp.append(i[3])
for i in allDict.keys():
y_major_locator = MultipleLocator(3)
ax = plt.gca()
ax.yaxis.set_major_locator(y_major_locator)
plt.tick_params(axis='both', labelsize=6)
plt.title(i)
plt.plot(timeStamp1, dut_target_temp, linewidth=1, linestyle='', marker='o', label='dut_target_temp')
plt.plot(timeStamp1, dut_temp, linewidth=1, linestyle='', marker='o', label='ntc_temp')
plt.plot(timeStamp1, ntc_temp, linewidth=1, linestyle='', marker='o', label='dut_temp')
plt.legend(loc='lower right') # 将图例放置右下角
# plt.savefig(a+'/'+i+".png")
print("wancheng")
plt.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
最终完成版:
效果图:
![](https://img-blog.csdnimg.cn/5bf28fd3ae4f499a8a790aba44b444ae.png)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(295, 316)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.y_but = QtWidgets.QPushButton(self.centralwidget)
self.y_but.setObjectName("y_but")
self.horizontalLayout.addWidget(self.y_but)
self.n_but_2 = QtWidgets.QPushButton(self.centralwidget)
self.n_but_2.setObjectName("n_but_2")
self.horizontalLayout.addWidget(self.n_but_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.verticalLayout.addWidget(self.plainTextEdit)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 295, 24))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textEdit.setPlaceholderText(_translate("MainWindow", "path:"))
self.y_but.setText(_translate("MainWindow", "yes"))
self.n_but_2.setText(_translate("MainWindow", "close"))
self.plainTextEdit.setPlaceholderText(_translate("MainWindow", "info:"))
run.py
import sys
import os
import re
import matplotlib.pylab as plt
from matplotlib.pyplot import MultipleLocator
from PyQt5.QtWidgets import QApplication, QMainWindow
from qtapp import Ui_MainWindow
import qtapp
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# 定义信号和槽函数
def signleAndSlot():
#点击yes按钮
UI.y_but.clicked.connect(clinky_but)
UI.n_but_2.clicked.connect(clinkn_but)
def clinkn_but():
sys.exit(app.exec_())
pass
# 定义yes按钮触发函数
def clinky_but():
file_tpye1 = '.csv'
file_tpye2 = '.xlsx'
file_pro = 'TecFineTune'
path = '/'+UI.textEdit.toPlainText().strip('file://')
paths = os.walk(path)
for root, dirs, files in paths:
for file in files:
if ((file_tpye1 in file) or (file_tpye2 in file)) and (file_pro in file):
pathes = os.path.join(root, file)
path1 = str(pathes)
print(path1)
try:
file = open(path1, "r") # 打开文件
file.readline() # 读取文件内容
allContect = file.read()
allList = re.findall(r'(.+),(.+),(.+),(.+),(.+),.+,.+,.+,.+,.+,.+,.+', allContect)
allDict = {}
file.close()
except:
UI.textEdit.toPlainText("mxksxkn")
for x in allList:
if x[2] != "NA" and x[3] != "NA" and x[4] != "NA":
if x[1] not in allDict:
allDict[x[1]] = []
allDict[x[1]].append([x[0], float(x[2]), float(x[3]), float(x[4])])
timeStamp1 = []
dut_target_temp = []
dut_temp = []
ntc_temp = []
for i in allDict[x[1]]:
timeStamp1.append(i[0])
dut_target_temp.append(i[1])
dut_temp.append(i[2])
ntc_temp.append(i[3])
for i in allDict.keys():
y_major_locator = MultipleLocator(3)
ax = plt.gca()
ax.yaxis.set_major_locator(y_major_locator)
plt.tick_params(axis='both', labelsize=6)
plt.title(i)
plt.plot(timeStamp1, dut_target_temp, linewidth=1, linestyle='', marker='o', label='dut_target_temp')
plt.plot(timeStamp1, dut_temp, linewidth=1, linestyle='', marker='o', label='ntc_temp')
plt.plot(timeStamp1, ntc_temp, linewidth=1, linestyle='', marker='o', label='dut_temp')
plt.legend(loc='lower right') # 将图例放置右下角
# plt.savefig(path+'/'+i+".png")
aa = path+'/'+i+".png"
print(aa,type(aa))
info = i+".png"+"已加载完成"
# UI.plainTextEdit.insertPlainText(info)
UI.plainTextEdit.appendPlainText(info)
plt.close()
UI.plainTextEdit.appendPlainText("所有的图片已经加载完毕")
if __name__ == '__main__':
#创建实例化窗口
app = QApplication(sys.argv)
main = MainWindow()
UI = main.ui
# 调用信号和槽的函数以及空间管理等函数
signleAndSlot()
main.show()
sys.exit(app.exec_())
#代码能够实现基本的需求,但是如果上传的路径下没有要求的文件会报错退出
使用try except优化:这样在输入错误路径的路径就不会报错退出
import sys
import os
import re
import matplotlib.pylab as plt
from matplotlib.pyplot import MultipleLocator
from PyQt5.QtWidgets import QApplication, QMainWindow
from qtapp import Ui_MainWindow
import qtapp
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# 定义信号和槽函数
def signleAndSlot():
#点击yes按钮
UI.y_but.clicked.connect(clinky_but)
UI.n_but_2.clicked.connect(clinkn_but)
def clinkn_but():
sys.exit(app.exec_())
pass
# 定义yes按钮触发函数
def clinky_but():
file_tpye1 = '.csv'
file_tpye2 = '.xlsx'
file_pro = 'TecFineTune'
path = '/'+UI.textEdit.toPlainText().strip('file://')
paths = os.walk(path)
try:
for root, dirs, files in paths:
for file in files:
if ((file_tpye1 in file) or (file_tpye2 in file)) and (file_pro in file):
pathes = os.path.join(root, file)
path1 = str(pathes)
file = open(path1, "r") # 打开文件
file.readline() # 读取文件内容
allContect = file.read()
allList = re.findall(r'(.+),(.+),(.+),(.+),(.+),.+,.+,.+,.+,.+,.+,.+', allContect)
allDict = {}
file.close()
for x in allList:
if x[2] != "NA" and x[3] != "NA" and x[4] != "NA":
if x[1] not in allDict:
allDict[x[1]] = []
allDict[x[1]].append([x[0], float(x[2]), float(x[3]), float(x[4])])
timeStamp1 = []
dut_target_temp = []
dut_temp = []
ntc_temp = []
for i in allDict[x[1]]:
timeStamp1.append(i[0])
dut_target_temp.append(i[1])
dut_temp.append(i[2])
ntc_temp.append(i[3])
for i in allDict.keys():
y_major_locator = MultipleLocator(3)
ax = plt.gca()
ax.yaxis.set_major_locator(y_major_locator)
plt.tick_params(axis='both', labelsize=6)
plt.title(i)
plt.plot(timeStamp1, dut_target_temp, linewidth=1, linestyle='', marker='o', label='dut_target_temp')
plt.plot(timeStamp1, dut_temp, linewidth=1, linestyle='', marker='o', label='ntc_temp')
plt.plot(timeStamp1, ntc_temp, linewidth=1, linestyle='', marker='o', label='dut_temp')
plt.legend(loc='lower right') # 将图例放置右下角
# plt.savefig(path+'/'+i+".png")
aa = path + '/' + i + ".png"
print(aa, type(aa))
info = i + ".png" + "已加载完成"
QApplication.processEvents()#刷新函数
# UI.plainTextEdit.insertPlainText(info)
UI.plainTextEdit.appendPlainText(info)
plt.close()
UI.plainTextEdit.appendPlainText("所有的图片已经加载完毕")
except UnboundLocalError:
UI.plainTextEdit.appendPlainText("请确认该路径下存在csv文件或者xlsx文件")
if __name__ == '__main__':
#创建实例化窗口
app = QApplication(sys.argv)
main = MainWindow()
UI = main.ui
# 调用信号和槽的函数以及空间管理等函数
signleAndSlot()
main.show()
sys.exit(app.exec_())
![](https://img-blog.csdnimg.cn/3af4ab488cd6479aa7692f0ba90c4434.png)
3 使用lua将指定文件路径下的csv文件中的数据读出,获取平均值放入指定路径下的csv文件
-- 需要操作的csv文件路径÷、
local tecTempRecordPath = "/Users/prm2160/Desktop/Double/PRM_BC7/0909/C07229200361V3G3Z/20220901_16-09-06.372-9621B0/user/TecFineTune_Fixture1_Unit1.csv"
-- 需要实现的csv文件路径÷、
local tecRBMLog = "/Users/prm2160/Desktop/Double/PRM_BC7/0909/C07229200361V3G3Z/20220901_16-09-06.372-9621B0/user/test1.csv"
local fTecRBMLog = io.open(tecRBMLog, "w")
if fTecRBMLog == nil then
error("error: " .. tecRBMLog .. " Path folder does not exist")
end
fTecRBMLog:write('TestName,Sensor1_Avg,Sensor2_Avg,Sensor3_Avg,Sensor1&2&3_Avg_Max,Sensor4_Max\r\n')
dut_cmd = {}
flag = false
testNameList = {}
local test_Name_All_Temp_Table = {}
local rbm_subsubtestname = {}
local tec_set_temp = {}
local function getAverage(tab)
local sum = 0
for i = 1, #tab do
sum = sum + tab[i]
end
local ret = tonumber(string.format("%.4f", sum / #tab))
return ret
end
for line in io.lines(tecTempRecordPath) do
if string.match(line, "RBM") then
local subsubtestname = string.match(line, ".+,(.+),.+,.+,.+,.+,.+,.+,.+,.+,.+,.+")--.+,.+,.+,.+,.+,.+,.+,(.+)
subsubtestname = string.gsub(subsubtestname, "\r", "")
subsubtestname = string.gsub(subsubtestname, "\n", "")
-- print(subsubtestname)
local curr_temp = string.match(line, ".+,.+,.+,(.+),.+,.+,.+,.+,.+,.+,.+,.+")
local targetemp = string.match(line, ".+,.+,(.+),.+,.+,.+,.+,.+,.+,.+,.+,.+")
curr_temp = tonumber(curr_temp)
targetemp = tonumber(targetemp)
local Sensor_1_temp = string.match(line, ".+,.+,.+,.+,(.+),.+,.+,.+,.+,.+,.+,.+")
local Sensor_2_temp = string.match(line, ".+,.+,.+,.+,.+,(.+),.+,.+,.+,.+,.+,.+")
local Sensor_3_temp = string.match(line, ".+,.+,.+,.+,.+,.+,(.+),.+,.+,.+,.+,.+")
local Sensor_4_temp = string.match(line, ".+,.+,.+,.+,.+,.+,.+,(.+),.+,.+,.+,.+")
bRet =true
for _, v in pairs(testNameList) do
if v == subsubtestname then
bRet = false
break
end
end
if bRet then
table.insert(testNameList, subsubtestname)
test_Name_All_Temp_Table[subsubtestname] = {}
test_Name_All_Temp_Table[subsubtestname]["Sensor_1"] = {}
test_Name_All_Temp_Table[subsubtestname]["Sensor_2"] = {}
test_Name_All_Temp_Table[subsubtestname]["Sensor_3"] = {}
test_Name_All_Temp_Table[subsubtestname]["Sensor_4"] = {}
end
if (targetemp~=nil and curr_temp~=nil) then
if(curr_temp>(targetemp*0.9)) then
if Sensor_1_temp and Sensor_2_temp and Sensor_3_temp and Sensor_4_temp then
table.insert(test_Name_All_Temp_Table[subsubtestname]["Sensor_1"], Sensor_1_temp)
table.insert(test_Name_All_Temp_Table[subsubtestname]["Sensor_2"], Sensor_2_temp)
table.insert(test_Name_All_Temp_Table[subsubtestname]["Sensor_3"], Sensor_3_temp)
table.insert(test_Name_All_Temp_Table[subsubtestname]["Sensor_4"], Sensor_4_temp)
else
error("Get sensor temp error!")
end end
end
end
end
local subsubtestnameTempMixMax = {}
for k, testName in pairs(testNameList) do
local sensor1_avg = getAverage(test_Name_All_Temp_Table[testName]["Sensor_1"])
local sensor2_avg = getAverage(test_Name_All_Temp_Table[testName]["Sensor_2"])
local sensor3_avg = getAverage(test_Name_All_Temp_Table[testName]["Sensor_3"])
local sensor123_avg_max = tonumber(string.format("%.4f", math.max(
table.unpack({sensor1_avg, sensor2_avg, sensor3_avg}))))
local sensor4_max = tonumber(string.format("%.4f", math.max(
table.unpack(test_Name_All_Temp_Table[testName]["Sensor_4"]))))
print(k,testName,sensor1_avg,sensor2_avg,sensor3_avg,sensor4_max)
local full_testname = testName
subsubtestnameTempMixMax[full_testname] = {}
table.insert(subsubtestnameTempMixMax[full_testname], {
["sensor1_avg"] = sensor1_avg,
["sensor2_avg"] = sensor2_avg,
["sensor3_avg"] = sensor3_avg,
["sensor123_avg_max"] = sensor123_avg_max,
["sensor4_max"] = sensor4_max
})
fTecRBMLog:write(full_testname .. "," .. sensor1_avg .. "," .. sensor2_avg .. "," .. sensor3_avg .. "," ..
sensor123_avg_max .. "," .. sensor4_max .. "\r\n")
end