gpt4 book ai didi

python - 使用 ElementTree 解析 xml 时,参数 1 出现意外类型错误

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

我使用这段代码在QGIS中制作插件,我尝试从本地驱动器打开xml,读取它然后解析它并在QLine中显示特定信息,我使用这段代码:

from PyQt4 import QtCore, QtGui
from ui_testparse import Ui_testparse
import xml.etree.ElementTree as ETree
# create the dialog for zoom to point


class testparseDialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_testparse()
self.ui.setupUi(self)

opendata = self.ui.btnCari
QtCore.QObject.connect(opendata, QtCore.SIGNAL('clicked()'),self.openxml)

def openxml(self, event=None):

#open dialog
openfile = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '*.xml')

self.ui.lineLokasi.setText(openfile)

#call XML data
self.isiData(openfile)

def isiData(self, nmsatu):
#open teks with read mode
openteks = open(nmsatu, 'r').read()

self.ui.textXml.setText(openteks)

#Parse XML from Above
self.parsenow(openteks)

def parsenow(self, parse):
element = ETree.fromstring(parse)
xml_obj = ETree.ElementTree(element)
for title_obj in xml_obj.findall('.//{http://www.isotc211.org/2005/gmd}dateStamp/'
'{http://www.isotc211.org/2005/gco}Date'):
print element
self.ui.lineSkala.setText(element)

但是当我尝试运行它时,我收到一条错误消息

Traceback (most recent call last):
File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 47, in openxml
self.isiData(openfile)
File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 56, in isiData
self.parsenow(openteks)
File "C:\Users\Mr.Pakde/.qgis2/python/plugins\testparse\testparsedialog.py", line 64, in parsenow
self.ui.lineSkala.setText(element)
TypeError: QLineEdit.setText(QString): argument 1 has unexpected type 'Element'

Python version:
2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]

有人可以帮我解决这个问题吗?

最佳答案

element 变量的类型为 Element,而 QLineEdit 中的 setText() 需要 QString 参数。当你:

print element

在调用 setText() 之前,它可能会打印 Element 类中的 0 __repr__ 方法返回的字符串

关于python - 使用 ElementTree 解析 xml 时,参数 1 出现意外类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23196522/

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