2013-11-28 5 views
1

PyQt 및 Matplotlib을 사용하여 2D CSV 파일을 그래프로 만드는 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_()) 

감사에 앞서 어떤 도움을위한 시간의 저를 놀리는 특정의 하나는

오류입니다.

+0

'parse_file '은'import numpy'를 사용하여 일반 파이썬 쉘에서 실행됩니까? 'Qstring'오류는 'Qt'와 관련된 것입니다. – hpaulj

+0

예. 위의 추가 컨텍스트에 대해 더 추가했습니다. – user3044129

답변

1

self.mpllineEdit.text()가 QString을 생성한다고 가정합니다. PyQt 문서 나 대화 형 셸에서 어떤 메소드를 가지고 있는지, 파이썬 문자열로 변환하기 위해 무엇인가를해야할 필요가 있는지 찾아야한다. genfromtxt가 해당 문자열을 줄로 나누고 줄 끝 문자를 제거하여 줄을 구문 분석 할 수 있습니다.

보십시오 : 일반 파이썬 문자열로 Qstring을 변환 할 수

self.parse_file(str(self.mpllineEdit.text())) 

.

+0

고마워요! 그게 해결 됐어! – user3044129

관련 문제