gpt4 book ai didi

python - 在 python 方法中使用 np.genfromtxt 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:24 25 4
gpt4 key购买 nike

我正在编写一个 python 脚本,它使用 PyQt 和 Matplotlib 来绘制 2D CSV 文件的图形。我仍在学习 python,所以我在解决一些错误时遇到了困难。尤其令我困扰的是

错误:

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_())

提前感谢您的帮助。

最佳答案

我假设 self.mpllineEdit.text() 生成一个 QString。您需要在 PyQt 文档或交互式 shell 中探索它有哪些方法,以及是否需要执行任何操作将其转换为常规 Python 字符串。 genfromtxt 尝试将该字符串拆分为行,然后去掉行结束字符,以便它可以解析该行。

尝试:

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

这可能会将 Qstring 转换为常规 Python 字符串。

关于python - 在 python 方法中使用 np.genfromtxt 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com