我在寫一個python腳本,它使用PyQt和Matplotlib來繪製一個2D CSV文件。即時通訊仍然學習Python,所以即時通訊工作中遇到的一些錯誤即時通訊。其中特別是困擾我的是在python方法中使用np.genfromtxt時遇到問題
錯誤:
Traceback (most recent call last): File "C:/Users/jonesza/Documents/Python Scripts/2D-Graph/Qt_2D_Plot.py", line 62, in update_graph l, v = self.parse_file(self.mpllineEdit.text()) File "C:/Users/jonesza/Documents/Python Scripts/2D-Graph/Qt_2D_Plot.py", line 53, in parse_file names=['time','temperature']) File "C:\WinPython\python-2.7.5.amd64\lib\site-packages\numpy\lib\npyio.py", line 1356, in genfromtxt first_values = split_line(first_line) File "C:\WinPython\python-2.7.5.amd64\lib\site-packages\numpy\lib_iotools.py", line 208, in _delimited_splitter line = line.strip(asbytes(" \r\n")) AttributeError: 'QString' object has no attribute 'strip'
的源代碼:提前任何幫助
# used to parse files more easily
from __future__ import with_statement
# Numpy module
import numpy as np
# for command-line arguments
import sys
# Qt4 bindings for core Qt functionalities (non-Gui)
from PyQt4 import QtCore
# Python Qt4 bindings for GUI objects
from PyQt4 import QtGui
# import the MainWindow widget from the converted .ui files
from qtdesigner import Ui_MplMainWindow
class DesignerMainWindow(QtGui.QMainWindow, Ui_MplMainWindow):
"""Customization for Qt Designer created window"""
def __init__(self, parent = None):
# initialization of the super class
super(DesignerMainWindow, self).__init__(parent)
# setup the GUI --> function generated by pyuic4
self.setupUi(self)
# connect the signals with the slots
QtCore.QObject.connect(self.mplpushButton, QtCore.
SIGNAL("clicked()"), self.update_graph)
QtCore.QObject.connect(self.mplactionOpen, QtCore.
SIGNAL("triggered()"), self.select_file)
QtCore.QObject.connect(self.mplactionQuit, QtCore.
SIGNAL("triggered()"), QtGui.qApp, QtCore.SLOT("quit()"))
def select_file(self):
"""opens a file select dialog"""
# open the dialog and get the selected file
file = QtGui.QFileDialog.getOpenFileName()
# if a file is selected
if file:
# update the lineEdit text with the selected filename
self.mpllineEdit.setText(file)
def parse_file(self, filename):
"""gets first two columns from .csv uploaded"""
#import data from .csv
data = np.genfromtxt(filename, delimiter=',',
names=['time','temperature'])
x = data['time']
y = data['temperature']
return x,y
def update_graph(self):
"""Updates the graph with new letteers frequencies"""
# get the axes for the 2D graph
l, v = self.parse_file(self.mpllineEdit.text())
# clear the Axes
self.mpl.canvas.ax.clear()
# plot the axes
self.mpl.canvas.ax.plot(l,v)
# force an image redraw
self.mpl.canvas.draw()
# create the GUI application
app = QtGui.QApplication(sys.argv)
# instantiate the main window
dmw = DesignerMainWindow()
# show it
dmw.show()
# start the Qt main loop execution, exiting from this script
# with the same return code of Qt application
sys.exit(app.exec_())
感謝。
'parse_file'是否在帶有'import numpy'的普通Python shell中運行? 'Qstring'錯誤指向涉及'Qt'的內容。 – hpaulj
是的,它的確如此。我爲上面的額外上下文添加了更多。 – user3044129